up previous next
SortBy

sort a list

Syntax
SortBy(L: LIST, CmpFn: FUNCTION)

Description
This function sorts the elements of the list in L with respect to the comparisons made by CmpFn; it overwrites L and returns NULL.

The comparison function CmpFn 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 to call SortBy(L,CmpFn) inside a function you will need to make the name CmpFn accessible using TopLevel CmpFn;

Note that if both CmpFn(A, B) and CmpFn(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"];
/**/  SortBy(ref M, ByLength);
/**/  M;
["mouse", "bird", "cat"]

See Also