({{, (RefLink[NumericalSort, paclet:ref/NumericalSort])[list] 
sorts the elements of list into numerical order.}})

OpenersNotesSection(Spacer1)Details

(RefLink[NumericalSort, paclet:ref/NumericalSort])[list] is equivalent to (RefLink[Sort, paclet:ref/Sort])[list,(RefLink[NumericalOrder, paclet:ref/NumericalOrder])].

(RefLink[NumericalSort, paclet:ref/NumericalSort]) uses the same canonical order as (RefLink[Sort, paclet:ref/Sort]), except that it treats all numeric expressions, dates and (RefLink[Quantity, paclet:ref/Quantity]) objects by value, independent of their representation.

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

OpenersPrimaryExamplesSection(Spacer1)Examples  (11)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Sort by numerical value:

NumericalSort[{1, Pi, E, Infinity, -Sqrt[2],-Infinity}]

This may be different from the canonical expression order:

Sort[{1, Pi, E, Infinity, -Sqrt[2],-Infinity}]

Groups of elements that can be compared with each other will typically be together in the result:

NumericalSort[{Now, TimeObject[], Infinity, Sqrt[2],-1,0, -Infinity, Quantity[1,"Meters"],  Quantity[3,"Feet"],  TimeObject[{1,23,45}],DateObject[{1618,3,3,9,8,8}]}]

OpenersExampleSection1(Spacer1)Scope  (7)

-∞ comes before any real-valued expression, and ∞ comes after any real-valued expression:

NumericalSort[{2,Infinity,0.,-GoldenRatio,-Infinity,1/3}]

Sort arguments of any expression:

NumericalSort[data[0,1,Infinity,-Infinity]]

Sort associations by numerical value:

NumericalSort[<|a->0,b->1,c->Infinity,d->-Infinity|>]

Sort expressions of the same head by numerical value:

NumericalSort[{h[0,2],h[Infinity,1],h[-Infinity,3,foo]}]

Complex-value expressions are sorted first by the real part and then by the absolute value of the imaginary part:

data=RandomSample@Flatten@Outer[Complex,Range[-1,1],Range[-2,2]]

n=Length[data]

ListLinePlot[Thread[Callout[ReIm[data],Range[n]]],PlotStyle->Opacity[0.3],Ticks->False]

sdata=NumericalSort[data]

ListLinePlot[Thread[Callout[ReIm[sdata],Range[n]]],PlotStyle->Opacity[0.3],Ticks->False]

Sort quantities having compatible units:

NumericalSort[{Quantity[50,"Centimeters"],Quantity[0,"Meters"],Quantity[1,"Yards"],Quantity[2,"Feet"]}]

If incompatible units are found, compatible units are grouped together:

NumericalSort[{Quantity[1,"Yards"],Quantity[1,"Minutes"],Quantity[50,"Centimeters"],Quantity[30,"Seconds"]}]

Sort date objects:

NumericalSort[{Tomorrow,Today,Yesterday}]

OpenersExampleSection3(Spacer1)Properties & Relations  (1)

(RefLink[NumericalSort, paclet:ref/NumericalSort]) uses (RefLink[NumericalOrder, paclet:ref/NumericalOrder]) as ordering function:

NumericalSort[{b,a,Infinity,0}]

Sort[{b,a,Infinity,0},NumericalOrder]

Using (RefLink[Less, paclet:ref/Less]) as ordering function also sorts numeric expressions, but not necessarily other expressions:

Sort[{b,a,Infinity,0},Less]

OpenersExampleSection4(Spacer1)Possible Issues  (1)

(RefLink[NumericalSort, paclet:ref/NumericalSort]) will not guarantee a particular ordering for different representations of the same number:

list={N[3,$MachinePrecision],3,3.,N[3,47]}

NumericalSort[list]

The canonical order will always rearrange in a definite way:

Sort[list]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Sort, paclet:ref/Sort])  ▪  (RefLink[SortBy, paclet:ref/SortBy])  ▪  (RefLink[NumericalOrder, paclet:ref/NumericalOrder])  ▪  (RefLink[AlphabeticSort, paclet:ref/AlphabeticSort])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Rearranging & Restructuring Lists, paclet:guide/RearrangingAndRestructuringLists])}})}})