Sort( list )
Sort( list, func )
Sort
sorts the list list in increasing order, using shellsort. In
the first form Sort
uses the operator <
to compare the elements. In
the second form Sort
uses the function func to compare elements.
This function must be a function taking two arguments that returns true
if the first is strictly smaller than the second and false
otherwise.
Sort
does not return anything, since it changes the argument list.
Use ShallowCopy
(see ShallowCopy) if you want to keep list. Use
Reversed
(see Reversed) if you want to get a new list sorted in
decreasing order.
It is possible to sort lists that contain multiple elements which compare
equal. It is not guaranteed that those elements keep their relative
order, i.e., Sort
is not stable.
gap> list := [ 5, 4, 6, 1, 7, 5 ];; Sort( list ); list; [ 1, 4, 5, 5, 6, 7 ] gap> list := [ [0,6], [1,2], [1,3], [1,5], [0,4], [3,4] ];; gap> Sort( list, function(v,w) return v*v < w*w; end ); list; [ [ 1, 2 ], [ 1, 3 ], [ 0, 4 ], [ 3, 4 ], [ 1, 5 ], [ 0, 6 ] ] # sorted according to the Euclidian distance from [0,0] gap> list := [ [0,6], [1,3], [3,4], [1,5], [1,2], [0,4], ];; gap> Sort( list, function(v,w) return v[1] < w[1]; end ); list; [ [ 0, 6 ], [ 0, 4 ], [ 1, 3 ], [ 1, 5 ], [ 1, 2 ], [ 3, 4 ] ] # note the random order of the elements with equal first component
SortParallel
(see SortParallel) allows you to sort a list and apply
the exchanges that are necessary to another list in parallel. Sortex
(see Sortex) sorts a list and returns the sorting permutation.
GAP 3.4.4