# Singular

#### D.2.3.14 sort

Procedure from library `general.lib` (see general_lib).

Usage:
sort(id[,v,o,n]); id = ideal/module/intvec/list
sort may be called with 1, 2 or 3 arguments in the following way:
sort(id[,v,n]); v=intvec of positive integers, n=integer,
sort(id[,o,n]); o=string (any allowed ordstr of a ring), n=integer

Return:
a list l of two elements:
 ``` l[1]: object of same type as input but sorted in the following way: - if id=ideal/module: generators of id are sorted w.r.t. intvec v (id[v[1]] becomes 1-st, id[v[2]] 2-nd element, etc.). If no v is present, id is sorted w.r.t. ordering o (if o is given) or w.r.t. actual monomial ordering (if no o is given): NOTE: generators with SMALLER(!) leading term come FIRST (e.g. sort(id); sorts backwards to actual monomial ordering) - if id=list or intvec: sorted w.r.t. < (indep. of other arguments) - if n!=0 the ordering is inverse, i.e. w.r.t. v(size(v)..1) default: n=0 l[2]: intvec, describing the permutation of the input (hence l[2]=v if v is given (with positive integers)) ```

Note:
If v is given id may be any simply indexed object (e.g. any list or string); if v[i]<0 and i<=size(id) v[i] is set internally to i; entries of v must be pairwise distinct to get a permutation id. Zero generators of ideal/module are deleted
If 'o' is given, the input is sorted by considering leading terms w.r.t. the new ring ordering given by 'o'

Example:
 ```LIB "general.lib"; ring r0 = 0,(x,y,z,t),lp; ideal i = x3,z3,xyz; sort(i); //sorts using lex ordering, smaller polys come first ==> [1]: ==> _[1]=z3 ==> _[2]=xyz ==> _[3]=x3 ==> [2]: ==> 2,3,1 sort(i,3..1); ==> [1]: ==> _[1]=xyz ==> _[2]=z3 ==> _[3]=x3 ==> [2]: ==> 3,2,1 sort(i,"ls")[1]; //sort w.r.t. negative lex ordering ==> _[1]=x3 ==> _[2]=xyz ==> _[3]=z3 intvec v =1,10..5,2..4;v; ==> 1,10,9,8,7,6,5,2,3,4 sort(v)[1]; // sort v lexicographically ==> 1,2,3,4,5,6,7,8,9,10 sort(v,"Dp",1)[1]; // sort v w.r.t (total sum, reverse lex) ==> 1,2,3,4,5,6,7,8,9,10 // Note that in general: lead(sort(M)) != sort(lead(M)), e.g: module M = [0, 1, 1, 0], [1, 0, 0, 1]; M; ==> M[1]=gen(3)+gen(2) ==> M[2]=gen(4)+gen(1) sort(lead(M), "c, dp")[1]; ==> _[1]=gen(4) ==> _[2]=gen(3) lead(sort(M, "c, dp")[1]); ==> _[1]=gen(3) ==> _[2]=gen(4) // In order to sort M wrt a NEW ordering by considering OLD leading // terms use one of the following equivalent commands: module( M[ sort(lead(M), "c,dp")[2] ] ); ==> _[1]=gen(4)+gen(1) ==> _[2]=gen(3)+gen(2) sort( M, sort(lead(M), "c,dp")[2] )[1]; ==> _[1]=gen(4)+gen(1) ==> _[2]=gen(3)+gen(2) // BUG: Please, don't use this sort for integer vectors or lists // with them if there can be negative integers! // TODO: for some HiWi sort(3..-3)[1]; ==> -3,-2,-1,0,1,2,3 sort(list(-v, v))[1]; ==> [1]: ==> -1,-10,-9,-8,-7,-6,-5,-2,-3,-4 ==> [2]: ==> 1,10,9,8,7,6,5,2,3,4 ```