CoCoA:SortBy

From ApCoCoAWiki

SortBy

sort a list

Description

This function sorts the elements of the list in V with respect

to the comparisons made by F; it overwrites V and returns NULL.

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 := [<quotes>dog</quotes>,<quotes>mouse</quotes>,<quotes>cat</quotes>];
  SortBy(M, Function(<quotes>ByLength</quotes>));
  M;
[<quotes>mouse</quotes>, <quotes>dog</quotes>, <quotes>cat</quotes>]
-------------------------------

Syntax

SortBy(V:LIST,F:FUNCTION):NULL

where V is a variable containing a list and F is a boolean-valued
comparison function of two arguments (e.g. representing <em>less than</em>).

Sort

Sorted

SortedBy

   <type>list</type>