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 LessByLength(S, T)    -- define the sorting function
/**/    Return len(S) < len(T);
/**/  EndDefine;

/**/  L := ["bird","mouse","cat", "elephant"];
/**/  sorted(L);  -- default is alphabetical order
["bird", "cat", "elephant", "mouse"]
/**/  SortedBy(L, LessByLength);
["cat", "bird", "mouse", "elephant"]

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

/**/  SortBy(ref L, LessByLength);  -- sort L in place, changing L
/**/  L;
["cat", "bird", "mouse", "elephant"]

See Also