This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Sort

Sort[list]
sorts the elements of list into canonical order.
Sort
sorts using the ordering function p.
  • Sort by default orders integers, rational, and approximate real numbers by their numerical values.
  • Sort orders complex numbers by their real parts, and in the event of a tie, by the absolute values of their imaginary parts.
  • Sort orders symbols by their names, and in the event of a tie, by their contexts.
  • Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth-first manner.
  • Sort treats powers and products specially, ordering them to correspond to terms in a polynomial.
  • Sort orders strings as in a dictionary, with uppercase versions of letters coming after lowercase ones. Sort places ordinary letters first, followed in order by script, Gothic, double-struck, Greek, and Hebrew. Mathematical operators appear in order of decreasing precedence.
  • Sort applies the function p to pairs of elements in list to determine whether they are in order. The default function p is OrderedQ[{#1, #2}]&.
  • Sort can be used on expressions with any head, not only List.
Sort a list:
Sort using Greater as the ordering function:
Sort by comparing the second part of each element:
Sort a list:
In[1]:=
Click for copyable input
Out[1]=
 
Sort using Greater as the ordering function:
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
 
Sort by comparing the second part of each element:
In[1]:=
Click for copyable input
Out[1]=
Sort any expressions:
Sort strings into dictionary order:
Sort by structure:
Sort by numerical value:
Sort expressions with any head:
Sort integers by magnitude:
Sort by absolute value:
Sort by structure:
Sort by numerical value:
Sort strings by dictionary order:
Sort strings by length:
Sort by norm:
Sort by real part:
Shorter expressions are sorted before longer ones:
Explicit numbers are sorted in numerical order:
Sort in reverse order:
Orderless functions sort in the same order as Sort:
Print each comparison done:
Collect a list of the comparisons done:
Sort[list] is equivalent to list[[Ordering[list]]]:
Numeric expressions are sorted by structure as well as numerical value:
Sort by numerical value only:
Elements whose order is not determined by the ordering function may not be kept in order:
Show comparisons made in doing a sort:
New in 1