Sort

Sort[list]

sorts the elements of list into canonical order.

Sort[list,p]

sorts using the ordering function p.

Details

  • 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. If a tie persists, they are ordered by 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 depthfirst 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, doublestruck, Greek, and Hebrew. Mathematical operators appear in order of decreasing precedence.
  • Sort[list,p] applies the ordering function p to pairs of elements in list to determine whether they are in order. The default function p is Order.
  • The ordering function p applied to a pair of elements e1, e2 may return either 1, 0, -1 or True, False. The value of p[e1,e2] is interpreted as follows:
  • 1e1 comes before e2
    0e1 and e2 should be treated as identical
    -1e1 comes after e2
    Truee1 and e2 are in order
    Falsee1 and e2 are out of order
  • If the ordering function p returns a value p[e1,e2] other than the preceding ones, then e1 and e2 are effectively treated as being in order.
  • Sort can be used on expressions with any head, not only List.

Examples

open allclose all

Basic Examples  (4)

Sort a list:

Sort a list of strings:

Sort by structure:

Sort by numerical value:

Sort by comparing the second part of each element:

Scope  (8)

Sort any expressions:

Sort works with any head, not just List:

Sort elements in an Association according to their values:

Sort using Greater as the ordering function:

Use GreaterEqual to maintain the relative order of equal elements:

Use NumericalOrder to allow complex numbers and number-like expressions:

Sort according to the rules of a particular language with AlphabeticOrder:

Define a custom ordering function that puts symbols ahead of numbers:

Use a pure function ordering function:

Applications  (5)

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:

Properties & Relations  (8)

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]]]:

Sort[list] is equivalent to SortBy[list,Identity]:

Possible Issues  (2)

Numeric expressions are sorted by structure as well as numerical value:

This order follows the normal rules for expressions based on their FullForm:

Sort by numerical value only:

Elements whose order is not determined by the ordering function may not be kept in order:

Neat Examples  (1)

Show comparisons made in doing a sort:

Wolfram Research (1988), Sort, Wolfram Language function, https://reference.wolfram.com/language/ref/Sort.html (updated 2017).

Text

Wolfram Research (1988), Sort, Wolfram Language function, https://reference.wolfram.com/language/ref/Sort.html (updated 2017).

CMS

Wolfram Language. 1988. "Sort." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/Sort.html.

APA

Wolfram Language. (1988). Sort. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Sort.html

BibTeX

@misc{reference.wolfram_2024_sort, author="Wolfram Research", title="{Sort}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/Sort.html}", note=[Accessed: 12-December-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_sort, organization={Wolfram Research}, title={Sort}, year={2017}, url={https://reference.wolfram.com/language/ref/Sort.html}, note=[Accessed: 12-December-2024 ]}