CoCoA:SortedBy

From ApCoCoAWiki
Revision as of 10:02, 24 October 2007 by XMLBot (talk | contribs) (pushing XML rev. 1.46, again)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

SortedBy

sort a list

Description

This function returns the list of the sorted elements of L without

affecting L, itself. As for <ttref>SortBy</ttref>, 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) &gt; Len(T);
  EndDefine;
  M := [<quotes>dog</quotes>,<quotes>mouse</quotes>,<quotes>cat</quotes>];
  SortedBy(M,Function(<quotes>ByLength</quotes>));
[<quotes>mouse</quotes>, <quotes>dog</quotes>, <quotes>cat</quotes>]
-------------------------------
  M;  -- M is not changed
[<quotes>dog</quotes>, <quotes>mouse</quotes>, <quotes>cat</quotes>]
-------------------------------
  Sorted(M);  -- the function <quotes>Sort</quotes> sorts using the default ordering:
              -- in this case, alphabetical order.
[<quotes>cat</quotes>, <quotes>dog</quotes>, <quotes>mouse</quotes>]
-------------------------------
  SortBy(M,Function(<quotes>ByLength</quotes>));  -- sort M in place, changing M
M;
[<quotes>mouse</quotes>, <quotes>dog</quotes>, <quotes>cat</quotes>]
-------------------------------

Syntax

SortedBy(L:LIST,F:FUNCTION):LIST

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

SortBy

   <type>list</type>