up previous next
SortedBy    --    sort a list


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

Description
This function returns the list of the sorted elements of L without affecting L, itself. As for SortBy , the comparison function LessThanFn 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: if both LessThanFn(A, B) and LessThanFn(B, A) return true, then A and B are viewed as being equivalent.

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