({{, (RefLink[NumericalOrder, paclet:ref/NumericalOrder])[(Subscript[e, 1]),(Subscript[e, 2])] 
gives 1 if (Subscript[e, 1])<(Subscript[e, 2]), -1 if (Subscript[e, 1])>(Subscript[e, 2]), 0 if (Subscript[e, 1]) and (Subscript[e, 2]) are identical or numerically the same and (RefLink[Order, paclet:ref/Order])[(Subscript[e, 1]),(Subscript[e, 2])] if (Subscript[e, 1]) and (Subscript[e, 2]) are not numerically comparable.}})

OpenersNotesSection(Spacer1)Details

(RefLink[NumericalOrder, paclet:ref/NumericalOrder]) provides a general alternative to canonical order in which numeric expressions, dates and (RefLink[Quantity, paclet:ref/Quantity]) objects are treated by value, but it is otherwise equivalent to canonical order.

(RefLink[Quantity, paclet:ref/Quantity]) expressions with compatible units are compared to each other by magnitude after converting them to a common unit.

(RefLink[DateObject, paclet:ref/DateObject]) expressions are compared to each other by (RefLink[AbsoluteTime, paclet:ref/AbsoluteTime]).

(RefLink[TimeObject, paclet:ref/TimeObject]) expressions are compared by (RefLink[AbsoluteTime, paclet:ref/AbsoluteTime]).

(RefLink[NumericalOrder, paclet:ref/NumericalOrder]) compares inexact numbers using all available significant digits. Unlike (RefLink[Equal, paclet:ref/Equal]), it does not allow any extra tolerance.

(RefLink[NumericalOrder, paclet:ref/NumericalOrder]) can be used as an ordering function in functions like (RefLink[Sort, paclet:ref/Sort]), (RefLink[OrderedQ, paclet:ref/OrderedQ]) or (RefLink[Ordering, paclet:ref/Ordering]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (20)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

These two numbers are not ordered:

NumericalOrder[3/2,1]

These two are numerically the same:

NumericalOrder[3/2,1.5]

Compare numeric expressions:

NumericalOrder[Pi,E^2]

This is not always the same as the canonical order of expressions:

Order[Pi,E^2]

Compare quantities:

NumericalOrder[Quantity[1,"Meters"], Quantity[3,"Feet"]]

Compare dates:

NumericalOrder[Now,Tomorrow]

OpenersExampleSection1(Spacer1)Scope  (6)

Compare any two numeric expressions:

NumericalOrder[Gamma[7/3],Sqrt[2]]

N[{Gamma[7/3],Sqrt[2]}]

-∞ comes before any real-valued expression:

NumericalOrder[-Infinity, GoldenRatio]

∞ comes after any real-valued expression:

NumericalOrder[Infinity, 0]

Complex valued expressions are ordered first by the real part:

NumericalOrder[Exp[I],Sin[1]]

When the real part is numerically the same, they are ordered by the absolute value of the imaginary part:

NumericalOrder[Exp[I],Cos[1]-10I]

Compare quantities of compatible units:

{q1=Quantity[2.,"Pounds"],q2=Quantity[1.,"Kilograms"]}

NumericalOrder[q1,q2]

The comparison is performed by converting into a common unit:

NumericalOrder[QuantityMagnitude[q1,"Kilograms"],QuantityMagnitude[q2,"Kilograms"]]

(RefLink[DateObject, paclet:ref/DateObject]) expressions are ordered by (RefLink[AbsoluteTime, paclet:ref/AbsoluteTime]):

{d1=Now, d2=DateObject[{2000,1,1,12,0,0}]}

NumericalOrder[d1,d2]

NumericalOrder[AbsoluteTime[d1],AbsoluteTime[d2]]

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

list={7,2/3,Infinity,Pi,-E,0,-Infinity}

slist=Sort[list,NumericalOrder]

Sort using the ordering permutation:

Ordering[list,All,NumericalOrder]

slist===list[[%]]

The resulting list is not ordered in canonical order, but it is ordered in numerical order:

OrderedQ[slist]

OrderedQ[slist,NumericalOrder]

OpenersExampleSection3(Spacer1)Applications  (1)

Get the numerical ordering for a list:

list = {1, Pi, E, Infinity, I Sqrt[-2],-Infinity};
order = Ordering[list,All, NumericalOrder]

list[[order]]

OpenersExampleSection4(Spacer1)Properties & Relations  (8)

For numeric expressions of different value, (RefLink[NumericalOrder, paclet:ref/NumericalOrder]) compares them using those values:

NumericalOrder[6,Pi]

NumericalOrder[6,N[Pi]]

(RefLink[Order, paclet:ref/Order]) always compares expressions structurally and may give different results:

Order[6,Pi]

Like (RefLink[Order, paclet:ref/Order]), (RefLink[NumericalOrder, paclet:ref/NumericalOrder]) is an antisymmetric function of expressions: (RefLink[NumericalOrder, paclet:ref/NumericalOrder])[(Subscript[e, 1]),(Subscript[e, 2])]==-NumericalOrder[(Subscript[e, 2]),(Subscript[e, 1])]:

e1=Sqrt[2]+5;
e2=E+Pi;

NumericalOrder[e1,e2]

NumericalOrder[e2,e1]

Unlike (RefLink[Order, paclet:ref/Order]), (RefLink[NumericalOrder, paclet:ref/NumericalOrder])[(Subscript[e, 1]),(Subscript[e, 2])] may return zero for non-identical (Subscript[e, 1]), (Subscript[e, 2]):

e1=1/2;
e2=0.5;

NumericalOrder[e1,e2]

Order[e1,e2]

For comparable expressions (Subscript[e, 1]), (Subscript[e, 2]) a result (RefLink[NumericalOrder, paclet:ref/NumericalOrder])[(Subscript[e, 1]),(Subscript[e, 2])]==0 implies (Subscript[e, 1])-(Subscript[e, 2])==0:

NumericalOrder[Pi,N[Pi]]

Pi-N[Pi]==0

(RefLink[NumericalOrder, paclet:ref/NumericalOrder]) compares inexact numbers using all available significant digits:

NumericalOrder[1.,1.+$MachineEpsilon]

NumericalOrder[1.,1.+$MachineEpsilon/2]

For machine-precision numbers, (RefLink[Less, paclet:ref/Less]), (RefLink[Equal, paclet:ref/Equal]), (RefLink[Greater, paclet:ref/Greater]), etc. use 7 bits of tolerance:

Less[1.,1.+2^7$MachineEpsilon]

Less[1.,1.+2^6$MachineEpsilon]

Inexact numbers with any other precision are compared up to that precision:

NumericalOrder[1.`10,1.`10+10^-10]

NumericalOrder[1.`10,1.`10+10^-10/2]

(RefLink[NumericalOrder, paclet:ref/NumericalOrder]) compares complex values by the real part and then by absolute value of the imaginary part:

NumericalOrder[1+I, 1-2 I]

This is consistent with (RefLink[Order, paclet:ref/Order]) for numbers:

Order[1+I, 1-2 I]

(RefLink[Less, paclet:ref/Less]), (RefLink[LessEqual, paclet:ref/LessEqual]) and related functions cannot compare complex numbers:

1+I<1-2I

Equivalent quantities have a (RefLink[NumericalOrder, paclet:ref/NumericalOrder]) of 0:

{q1=Quantity[60, "Seconds"],q2=Quantity[1,"Minute"]}

NumericalOrder[q1, q2]

The canonical order distinguishes between the two representations:

Order[q1, q2]

Use (RefLink[Equal, paclet:ref/Equal]) to show that they are indeed equivalent quantities:

q1==q2

For non-numerical expressions (Subscript[e, 1]), (Subscript[e, 2]), (RefLink[NumericalOrder, paclet:ref/NumericalOrder]) coincides with (RefLink[Order, paclet:ref/Order]):

NumericalOrder[foo,bar]

Order[foo,bar]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

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

list = {Pi,N[3,$MachinePrecision], N[Pi], 3, E, 3., N[E,27],N[3,20]}

slist = Sort[list,NumericalOrder]

This does not give the same result for a permutation of the list:

plist = RandomSample[list];slist===Sort[plist,NumericalOrder]

The canonical order will rearrange in a definite way:

Sort[list]

%===Sort[plist]

A stricter order can be defined by using (RefLink[Order, paclet:ref/Order]) to resolve cases where (RefLink[NumericalOrder, paclet:ref/NumericalOrder]) gives 0:

strictNumericalOrder[e1_,e2_] :=With[{no = NumericalOrder[e1, e2]},If[no==0,Order[e1,e2],no]]

Sort[list,strictNumericalOrder]

%===Sort[plist,strictNumericalOrder]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[NumericalSort, paclet:ref/NumericalSort])  ▪  (RefLink[Sort, paclet:ref/Sort])  ▪  (RefLink[Order, paclet:ref/Order])  ▪  (RefLink[AlphabeticOrder, paclet:ref/AlphabeticOrder])  ▪  (RefLink[Less, paclet:ref/Less])  ▪  (RefLink[Greater, paclet:ref/Greater])  ▪  (RefLink[Equal, paclet:ref/Equal])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Numerical Functions, paclet:guide/NumericalFunctions])}})}})