up previous next
SortBy    --    sort a list


Syntax
SortBy(ref L: LIST, LessThanFunc: FUNCTION)

Description
This procedure sorts the elements of the list in L into increasing order with respect to the comparisons made by LessThanFunc; it overwrites L and returns nothing.

The comparison function LessThanFunc takes two arguments and must return true if the first argument is less than the second, otherwise it must return false.

If you want to call SortBy(ref L,LessThanFunc) inside a function, and you have defined LessThanFunc at top level (rather than as an anonymous function), then you need to ensure that the name LessThanFunc is visible using TopLevel LessThanFunc;

NOTE: if both LessThanFunc(A, B) and LessThanFunc(B, A) return true, then A and B are viewed as being equivalent.

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

/**/  L := ["bird", "mouse", "cat", "elephant"];
/**/  SortBy(ref L, LessThanLen);
/**/  L;
["cat", "bird", "mouse", "elephant"]

See Also