next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Macaulay2Doc :: sort(List)

sort(List) -- sort a list



The sort function uses <= to compare elements of the list, which in turn calls upon the comparison operator ?, so, to determine how the elements are sorted, refer to its documentation. Methods for new user defined types of objects can be installed for it, too, to obtain the desired sorting behavior.

i1 : sort {c,e,a,f,b,f}

o1 = {a, b, c, e, f, f}

o1 : List
i2 : sort {4,2,6,3,8,2}

o2 = {2, 2, 3, 4, 6, 8}

o2 : List

Comparison of strings is implemented so that symbols come before alphanumeric characters, upper and lower case characters are sorted together, and strings of digits are sorted by size of the implied number.

i3 : sort {"a11","a1","a2","A11","A1","A2","B2"}

o3 = {A1, a1, A2, a2, A11, a11, B2}

o3 : List

Visible lists (lists, sequences, and arrays) are compared lexicographically.

i4 : sort {(1,1),(2,1),(3,1),(1,2),(3,2)}

o4 = {(1, 1), (1, 2), (2, 1), (3, 1), (3, 2)}

o4 : List

If the elements of the list are polynomials, then the monomial order is used to sort the elements according to their lead terms.

i5 : R = ZZ[a..d,MonomialOrder=>Lex];
i6 : sort{a*d, b^100, c^3*d}

       3    100
o6 = {c d, b   , a*d}

o6 : List

The optional arguments are not used in this version of sort.


If a list contains elements of different types, the sorting still occurs, but might not be particularly intuitive.

See also