({{, (RefLink[Sort, paclet:ref/Sort])[list]
sorts the elements of list into canonical order. }, {, (RefLink[Sort, paclet:ref/Sort])[list,p]
sorts using the ordering function p. }})

OpenersNotesSection(Spacer1)Details

(RefLink[Sort, paclet:ref/Sort]) by default orders integers, rational, and approximate real numbers by their numerical values.

(RefLink[Sort, paclet:ref/Sort]) orders complex numbers by their real parts, and in the event of a tie, by the absolute values of their imaginary parts.

(RefLink[Sort, paclet:ref/Sort]) orders symbols by their names, and in the event of a tie, by their contexts.

(RefLink[Sort, paclet:ref/Sort]) usually orders expressions by putting shorter ones first, and then comparing parts in a depth-first manner.

(RefLink[Sort, paclet:ref/Sort]) treats powers and products specially, ordering them to correspond to terms in a polynomial.

(RefLink[Sort, paclet:ref/Sort]) orders strings as in a dictionary, with uppercase versions of letters coming after lowercase ones. (RefLink[Sort, paclet:ref/Sort]) places ordinary letters first, followed in order by script, Gothic, double-struck, Greek, and Hebrew. Mathematical operators appear in order of decreasing precedence.

(RefLink[Sort, paclet:ref/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 (RefLink[Order, paclet:ref/Order]).

The ordering function p applied to a pair of elements (Subscript[e, 1]), (Subscript[e, 2]) may return either 1, 0, -1 or (RefLink[True, paclet:ref/True]), (RefLink[False, paclet:ref/False]). The value of p[(Subscript[e, 1]),(Subscript[e, 2])] is interpreted as follows:

(RefLink[Sort, paclet:ref/Sort]) can be used on expressions with any head, not only (RefLink[List, paclet:ref/List]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (24)

OpenersExampleSection0(Spacer1)Basic Examples  (5)

Sort a list:

Sort[{d,b,c,a}]

Sort using (RefLink[Greater, paclet:ref/Greater]) as the ordering function:

Sort[{4,1,3,2,2},Greater]

Sort[{4,1,3,2,2},#1>#2&]

Sort by comparing the second part of each element:

Sort[{{a,2},{c,1},{d,3}},#1[[2]]<#2[[2]]&]

Sort elements in an (RefLink[Association, paclet:ref/Association]) according to their values:

Sort[<|a->4,b->1,c->3,d->2,e->2|>]

Sort (RefLink[Association, paclet:ref/Association]) in reverse order:

Sort[<|a->4,b->1,c->3,d->2,e->2|>,Greater]

OpenersExampleSection1(Spacer1)Scope  (3)

Sort any expressions:

Sort[{y,x^2,x+y,y^3}]

Sort strings into dictionary order:

Sort[{"cat", "fish", "catfish", "Cat"}]

Sort by structure:

Sort[{Pi,E,2,3,1,Sqrt[2]}]

Sort by numerical value:

Sort[{Pi,E,2,3,1,Sqrt[2]},Less]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

Sort expressions with any head:

Sort[f[4,2,1,3]]

Sort[b.c.d.a]

OpenersExampleSection3(Spacer1)Applications  (5)

Sort integers by magnitude:

Sort[{-11,10,2,1,-4}]

Sort by absolute value:

Sort[{-11,10,2,1,-4},Abs[#1]<Abs[#2]&]

Sort by structure:

Sort[Join[Log[Range[10]],Sqrt[Range[10]]]]

Sort by numerical value:

Sort[Join[Log[Range[10]],Sqrt[Range[10]]],Less]

Sort strings by dictionary order:

Sort[{"aa","abb","ba","b","aaa"}]

Sort strings by length:

Sort[{"aa","abb","ba","b","aaa"},StringLength[#1]<StringLength[#2]&]

Sort by norm:

RandomInteger[{-5,5},{10,3}]

Sort[%,Norm[#1]<Norm[#2]&]

Sort by real part:

Sort[{I,1+I,1-I,2+3I},Re[#1]<Re[#2]&]

OpenersExampleSection4(Spacer1)Properties & Relations  (7)

Shorter expressions are sorted before longer ones:

Sort[{a+b+c,a+b,a+c}]

Explicit numbers are sorted in numerical order:

Sort[{5.6,5,2.3,-1,4/3,1}]

Sort in reverse order:

Sort[{a+b+c,a+b,a+c},OrderedQ[{#2,#1}]&]

Orderless functions sort in the same order as (RefLink[Sort, paclet:ref/Sort]):

(a+b+c)(a+b)(a+c)

Print each comparison done:

Sort[{4,1,3,2},(Print[{#1,#2}];#1>#2)&]

Collect a list of the comparisons done:

Reap[Sort[{4,1,3,2},(Sow[{#1,#2}];#1>#2)&]]

(RefLink[Sort, paclet:ref/Sort])[list] is equivalent to list[[(RefLink[Ordering, paclet:ref/Ordering])[list]]]:

list = RandomInteger[10,10];

Sort[list]

list[[Ordering[list]]]

OpenersExampleSection5(Spacer1)Possible Issues  (2)

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

Sort[{∞,Sqrt[2],1,2,-∞,1/Sqrt[2]}]

Sort by numerical value only:

Sort[{∞,Sqrt[2],1,2,-∞,1/Sqrt[2]},Less]

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

Sort[{4,1,3,7,9},#>3&]

Sort[{4,3,1,7,9},#>3&]

OpenersExampleSection6(Spacer1)Neat Examples  (1)

Show comparisons made in doing a sort:

ListPlot[Reap[Sort[Table[Round[10Sin[t]],{t,0,50}],(Sow[#1];#1>#2)&]][[2,1]]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ReverseSort, paclet:ref/ReverseSort])  ▪  (RefLink[SortBy, paclet:ref/SortBy])  ▪  (RefLink[NumericalSort, paclet:ref/NumericalSort])  ▪  (RefLink[AlphabeticSort, paclet:ref/AlphabeticSort])  ▪  (RefLink[TakeSmallest, paclet:ref/TakeSmallest])  ▪  (RefLink[TakeLargest, paclet:ref/TakeLargest])  ▪  (RefLink[Ordering, paclet:ref/Ordering])  ▪  (RefLink[Order, paclet:ref/Order])  ▪  (RefLink[OrderedQ, paclet:ref/OrderedQ])  ▪  (RefLink[Orderless, paclet:ref/Orderless])  ▪  (RefLink[Median, paclet:ref/Median])  ▪  (RefLink[Quantile, paclet:ref/Quantile])  ▪  (RefLink[Nearest, paclet:ref/Nearest])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Rearranging Lists, paclet:tutorial/RearrangingLists])}, {▪(RefLinkPlain[Ordering in Lists, paclet:tutorial/OrderingInLists])}, {▪(RefLinkPlain[Structural Operations, paclet:tutorial/StructuralOperations])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Rearranging & Restructuring Lists, paclet:guide/RearrangingAndRestructuringLists])}, {▪(RefLinkPlain[Permutations, paclet:guide/Permutations])}, {▪(RefLinkPlain[Structural Operations on Expressions, paclet:guide/StructuralOperationsOnExpressions])}, {▪(RefLinkPlain[Discrete Mathematics, paclet:guide/DiscreteMathematics])}, {▪(RefLinkPlain[Math & Counting Operations on Lists, paclet:guide/MathematicalAndCountingOperationsOnLists])}, {▪(RefLinkPlain[Descriptive Statistics, paclet:guide/DescriptiveStatistics])}, {▪(RefLinkPlain[Associations, paclet:guide/Associations])}, {▪(RefLinkPlain[Computation with Structured Datasets, paclet:guide/ComputationWithStructuredDatasets])}, {▪(RefLinkPlain[List Manipulation, paclet:guide/ListManipulation])}, {▪(RefLinkPlain[Discrete & Integer Data, paclet:guide/DiscreteAndIntegerData])}, {▪(RefLinkPlain[Operations on Sets, paclet:guide/OperationsOnSets])}, {▪(RefLinkPlain[Handling Arrays of Data, paclet:guide/HandlingArraysOfData])}, {▪(RefLinkPlain[Numerical Data, paclet:guide/NumericalData])}, {▪(RefLinkPlain[Robust Descriptive Statistics, paclet:guide/RobustDescriptiveStatistics])}, {▪(RefLinkPlain[String Operations, paclet:guide/StringOperations])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/05-operations-on-lists.html])(WebLink[: Operations on Lists, https://www.wolfram.com/language/elementary-introduction/05-operations-on-lists.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/11-strings-and-text.html])(WebLink[: Strings and Text, https://www.wolfram.com/language/elementary-introduction/11-strings-and-text.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/30-rearranging-lists.html])(WebLink[: Rearranging Lists, https://www.wolfram.com/language/elementary-introduction/30-rearranging-lists.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/34-associations.html])(WebLink[: Associations, https://www.wolfram.com/language/elementary-introduction/34-associations.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/46-writing-good-code.html])(WebLink[: Writing Good Code, https://www.wolfram.com/language/elementary-introduction/46-writing-good-code.html])}, {(WebLink[DocumentationFooterIconsRelatedLinks-NKS.png(Spacer1)NKS|Online, http://www.wolframscience.com/nksonline/index/search.cgi?SearchIndex=Sort]) ((WebLink[A New Kind of Science, http://www.wolframscience.com/nksonline/]))}})}})