List operation problem

2
If I have a list of objects in a certain sorting and I do a list operation on this list should the result not be sorted in the same way as the original list? To clarify. A new list can be created with a certain sorting. But when I do a list operation (substract something from this list), the new list is not sorted and currently I am unable to change that sorting of the new list. And because the newly created list had a different sorting I got an error in the following microflow. This microflow calculates haversine distances between two coordinates. And because calculating between point 1 and 2 is the same as between 2 and 1 I wanted to be efficient. For calculating every distance between every two points you only need to do about halve the calculations. Like this. 1 to 2 3 4 5 2 to 3 4 5 3 to 4 5 4 to 5 So create a list of all the points. Create a new list and substract the first (that will be the to list). Create a new list and substract the last (that will be the from list). Then do an iteration within a iteration where in the first iteration you substract add the end of this part this object from the to list. Unfortunately the sorting goes off when doing the list operation and you get unexpected results (and my first reference to the new scala language in the stack trace :) ). In my opinion I should either be able to influence the sorting of the list after a list operation or it should follow the sorting from start list. For now I have to forget about efficiency (unless somebody has a good idea). Regards, Ronald PS And by the way, I probably will update the google map widget to incorporate the java action for haversine distance. I have also done for another project route distance calculation so it is about time I think to give something back to the community.
asked
3 answers
1

When doing list operations basically the lists are turned into sets. We specifically don't make a difference between lists and sets because it would be complex for the more business-oriented developers. However I don't see an immediate reason why we could not use a SortedSet or something similar so you could file a feature request to have lists maintain their sorting after using list operations (where possible, for example a Union might not be so straightforward without being able to specifically give an attribute to sort on, so I'd say they should just follow each other, like list a + list b, minus duplicates.)

Or add the request to specifically sort a list based on some attribute.

answered
0

You could sort the resulting list in a simple java action after the list operations.

answered
0

I am not an experienced java programmer so here is the code I used to sort.

// This file was generated by Mendix Business Modeler 4.0.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package locaties.actions;

import java.util.Collections;
import java.util.Comparator;
import locaties.proxies.Locatie;
import com.mendix.systemwideinterfaces.core.UserAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

/**
 * Sorts the list in Java
 */
public class ListSort extends UserAction<java.util.List<IMendixObject>>
{
    private java.util.List<IMendixObject> __List;
    private java.util.List<locaties.proxies.Locatie> List;

    public ListSort(java.util.List<IMendixObject> List)
    {
        super();
        this.__List = List;
    }

    @Override
    public java.util.List<IMendixObject> executeAction() throws Exception
    {
        this.List = new java.util.ArrayList<locaties.proxies.Locatie>();
        if (__List != null)
            for (IMendixObject __ListElement : __List)
                this.List.add(locaties.proxies.Locatie.initialize(getContext(), __ListElement));

        // BEGIN USER CODE
        Collections.sort(List, new Comparator<Locatie>(){
                 public int compare(Locatie a,  Locatie b){
                 return a.getLocatieID().compareTo(b.getLocatieID());
                         }
                    });
        return __List;
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @Override
    public String toString()
    {
        return "ListSort";
    }

    // BEGIN EXTRA CODE
    // END EXTRA CODE
}
// This file was generated by Mendix Business Modeler 4.0.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package locaties.actions;

import java.util.Collections;
import java.util.Comparator;
import locaties.proxies.Locatie;
import com.mendix.systemwideinterfaces.core.UserAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;

/**
 * Sorts the list in Java
 */
public class ListSort extends UserAction<java.util.List<IMendixObject>>
{
    private java.util.List<IMendixObject> __List;
    private java.util.List<locaties.proxies.Locatie> List;

    public ListSort(java.util.List<IMendixObject> List)
    {
        super();
        this.__List = List;
    }

    @Override
    public java.util.List<IMendixObject> executeAction() throws Exception
    {
        this.List = new java.util.ArrayList<locaties.proxies.Locatie>();
        if (__List != null)
            for (IMendixObject __ListElement : __List)
                this.List.add(locaties.proxies.Locatie.initialize(getContext(), __ListElement));

        // BEGIN USER CODE
        Collections.sort(List, new Comparator<Locatie>(){
                 public int compare(Locatie a,  Locatie b){
                 return a.getLocatieID().compareTo(b.getLocatieID());
                         }
                    });
        return __List;
        // END USER CODE
    }

    /**
     * Returns a string representation of this action
     */
    @Override
    public String toString()
    {
        return "ListSort";
    }

    // BEGIN EXTRA CODE
    // END EXTRA CODE
}

I have the entity Locatie with the attribute LocatieID which I would want to use to sort on. I still got this error: - Comparator cannot be resolved to a type - The method sort(List<t>, Comparator) in the type Collections is not applicable for the arguments (List<locatie>, new Comparator<locatie>(){}) I tried to use LocatieID instead of Locatie but that does not help. Any suggetions?

[EDIT] I changed the code. Now it works (the sorting that is). I still get an unexpected error. But that may be a Mendix bug.

answered