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.
You could sort the resulting list in a simple java action after the list operations.
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.