up previous next
SortedBy

sort a list

Syntax
SortedBy(L: LIST, F: FUNCTION): LIST

Description
This function returns the list of the sorted elements of L without affecting L, itself. As for SortBy , 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.

Example
/**/  Define ByLength(S, T)    -- define the sorting function
/**/    Return len(S) > len(T);
/**/  EndDefine;

/**/  M := ["bird","mouse","cat"];
/**/  SortedBy(M, ByLength);
["mouse", "bird", "cat"]

/**/  M;  -- M is not changed
["bird", "mouse", "cat"]

/**/  sorted(M);  -- the function "Sort" sorts using the default ordering:
                  -- in this case, alphabetical order.
["cat", "bird", "mouse"]

/**/  SortBy(ref M, ByLength);  -- sort M in place, changing M
/**/  M;
["mouse", "bird", "cat"]

See Also