SORT INDEX

SORT _ _ _ _ _ _ _ _ _ _ _ _ operator

The sort operator sorts the elements of a list according to an arbitrary comparison operator.

syntax:

sort(<lst>,<comp>)

<lst> is a list of algebraic expressions. <comp> is a comparison operator which defines a partial ordering among the members of <lst>. <comp> may be one of the builtin comparison operators like <( lessp), <=( leq) etc., or <comp> may be the name of a comparison procedure. Such a procedure has two arguments, and it returns true if the first argument ranges before the second one, and 0 or nil otherwise. The result of sort is a new list which contains the elements of <lst> in a sequence corresponding to <comp>.

examples:


 procedure ce(a,b);

   if evenp a and not evenp b then 1 else 0;

for i:=1:10 collect random(50)$

sort(ws,>=); 

  {41,38,33,30,28,25,20,17,8,5}


sort(ws,<); 

  {5,8,17,20,25,28,30,33,38,41}


sort(ws,ce); 

  {8,20,28,30,38,5,17,25,33,41}


  procedure cd(a,b);

  if deg(a,x)>deg(b,x) then 1 else

  if deg(a,x)<deg(b,x) then 0 else

  if deg(a,y)>deg(b,y) then 1 else 0;

sort({x^2,y^2,x*y},cd);

    2      2
  {x ,x*y,y }