Sort Built-ins¶
Sort Built-ins sort lists by passing a function that customizes how the sorting will happen.
Syntax
1 | <sortedObjects List> := sort(<list List<String>>, <comparator (A,B) -> Number>) |
Returns
The sorted list of objects.
Parameters
-
list - The list to be sorted
-
comparator - The function's reference that defines the sorting mechanism
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | model Example ... comparator := Example::compareTo sortedObjects := sort(objects, comparator) ... func compareTo(left,right) aCompare := left.a.compareTo(right.a) if (aCompare = 0) then return left.b.compareTo(right.b) else return aCompare end end |
The example comparison function above takes two objects. It returns one of:
-
0
if they are equal. -
A negative result if the first object should be considered before the second.
-
A positive result otherwise.
Convenience Method for Sorting¶
A convenience method to sort list by their natural ordering. It sorts lists that contain strings or numbers.
Syntax
1 | <sortedObjects List> := sort(<list List<String>>) |
Returns
The sorted list of objects.
Parameter
- list - The list to be sorted
Example
1 2 3 | list := ["a","c","b"] sortedObjects := sort(list) // sortedObjects = ["a", "b", "c"] |