File r37/lisp/csl/html/r37_0180.html artifact 0e058b3c5a part of check-in dc9552dc80



<A NAME=SORT>

<TITLE>SORT</TITLE></A>
<b><a href=r37_idx.html>INDEX</a></b><p><p>



<B>SORT</B> _ _ _  _ _ _  _ _ _  _ _ _ <B>operator</B><P>
<P>
 
 <P>
<P>
The <em>sort</em> operator sorts the elements of a list according to 
an arbitrary comparison operator. 
 <P> <H3> 
syntax: </H3>
<P>
<P>
<em>sort</em>(&lt;lst&gt;,&lt;comp&gt;) 
<P>
<P>
<P>
&lt;lst&gt; is a 
<A HREF=r37_0053.html>list</A> of algebraic expressions. 
&lt;comp&gt; is a comparison operator which defines a partial 
ordering among the members of &lt;lst&gt;. &lt;comp&gt; may be 
one of the builtin comparison operators like 
<em>&lt;</em>(
<A HREF=r37_0115.html>lessp</A>), <em>&lt;=</em>(
<A HREF=r37_0114.html>leq</A>) 
etc., or &lt;comp&gt; may be the name of a comparison procedure. 
Such a procedure has two arguments, and it returns 

<A HREF=r37_0122.html>true</A> if the first argument 
ranges before the second one, and 0 or 
<A HREF=r37_0014.html>nil</A> otherwise. 
The result of <em>sort</em> is a new list which contains the 
elements of &lt;lst&gt; in a sequence corresponding to &lt;comp&gt;. 
 <P> <H3> 
examples: </H3>
<P><PRE><TT>
 procedure ce(a,b);

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

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

sort(ws,&gt;=); 

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


sort(ws,&lt;); 

  {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)&gt;deg(b,x) then 1 else

  if deg(a,x)&lt;deg(b,x) then 0 else

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

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

    2      2
  {x ,x*y,y }

</TT></PRE><P><P>
<P>


REDUCE Historical
REDUCE Sourceforge Project | Historical SVN Repository | GitHub Mirror | SourceHut Mirror | NotABug Mirror | Chisel Mirror | Chisel RSS ]