The first function sorts the elements of the list in V with respect
to the comparisons made by F; it overwrites V. The second function,
'Sorted', returns the list of the sorted elements of L without
affecting L, itself. The comparison function F takes two arguments
and returns TRUE if the first argument is less than the second, otherwise
it returns FALSE. The sorted list is in increasing order. Note that if
both F(A,B) and F(B,A) return TRUE, then A and B are viewed as being equal.
|
Define ByLength(S,T) -- define the sorting function
Return Len(S) > Len(T);
EndDefine;
M := ['dog','mouse','cat'];
SortedBy(M,Function('ByLength'));
["mouse", "dog", "cat"]
-------------------------------
M; -- M is not changed
["dog", "mouse", "cat"]
-------------------------------
Sorted(M); -- the function 'Sort' sorts using the default ordering:
-- in this case, alphabetical order.
["cat", "dog", "mouse"]
-------------------------------
SortBy(M,Function('ByLength')); -- sort M in place, changing M
M;
["mouse", "dog", "cat"]
-------------------------------
|