NumericalOrder[e1,e2]
gives 1 if e1<e2, -1 if e1>e2, 0 if e1 and e2 are numerically the same, and orders by type or using canonical order if e1 and e2 are not numerically comparable.
NumericalOrder
NumericalOrder[e1,e2]
gives 1 if e1<e2, -1 if e1>e2, 0 if e1 and e2 are numerically the same, and orders by type or using canonical order if e1 and e2 are not numerically comparable.
Details
- NumericalOrder provides a general alternative to canonical order in which numeric expressions, dates and Quantity objects are treated by value.
- Numeric expressions are sorted by real part first, and by absolute value of the imaginary part if the real parts coincide.
- Quantity expressions with compatible units are compared to each other by magnitude after converting them to a common unit.
- DateObject expressions are compared to each other by AbsoluteTime.
- TimeObject expressions are compared to each other by AbsoluteTime.
- If e1 and e2 are both numerical but cannot be compared, they will be sorted in the order numeric expressions, quantities, dates and times.
- Numerical expressions always precede non-numerical ones.
- If neither e1 nor e2 has a numerical value, NumericalOrder[e1,e2] is the same as Order[e1,e2].
- NumericalOrder compares inexact numbers using all available significant digits. Unlike Equal, it does not allow any extra tolerance.
- NumericalOrder can be used as an ordering function in functions like Sort, OrderedQ or Ordering.
Examples
open all close allBasic Examples (4)
These two numbers are not ordered:
NumericalOrder[3 / 2, 1]These two are numerically the same:
NumericalOrder[3 / 2, 1.5]NumericalOrder[Pi, E ^ 2]This is not always the same as the canonical order of expressions:
Order[Pi, E ^ 2]NumericalOrder[Quantity[1, "Meters"], Quantity[3, "Feet"]]NumericalOrder[Now, Tomorrow]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"]]DateObject expressions are ordered by AbsoluteTime:
{d1 = Now, d2 = DateObject[{2000, 1, 1, 12, 0, 0}]}NumericalOrder[d1, d2]NumericalOrder[AbsoluteTime[d1], AbsoluteTime[d2]]Use 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]Applications (1)
Properties & Relations (8)
For numeric expressions of different value, NumericalOrder compares them using those values:
NumericalOrder[6, Pi]NumericalOrder[6, N[Pi]]Order always compares expressions structurally and may give different results:
Order[6, Pi]Like Order, NumericalOrder is an antisymmetric function of expressions: NumericalOrder[e1,e2]==-NumericalOrder[e2,e1]:
e1 = Sqrt[2] + 5;
e2 = E + Pi;NumericalOrder[e1, e2]NumericalOrder[e2, e1]Unlike Order, NumericalOrder[e1,e2] may return zero for non-identical e1, e2:
e1 = 1 / 2;
e2 = 0.5;NumericalOrder[e1, e2]Order[e1, e2]For comparable expressions e1, e2 a result NumericalOrder[e1,e2]0 implies e1-e2==0:
NumericalOrder[Pi, N[Pi]]Pi - N[Pi] == 0NumericalOrder compares inexact numbers using all available significant digits:
NumericalOrder[1., 1. + $MachineEpsilon]NumericalOrder[1., 1. + $MachineEpsilon / 2]For machine-precision numbers, Less, Equal, 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]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 Order for numbers:
Order[1 + I, 1 - 2 I]Less, LessEqual and related functions cannot compare complex numbers:
1 + I < 1 - 2IEquivalent quantities have a 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 Equal to show that they are indeed equivalent quantities:
q1 == q2For non-numerical expressions e1, e2, NumericalOrder coincides with Order:
NumericalOrder[foo, bar]Order[foo, bar]Possible Issues (1)
Sorting with 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 Order to resolve cases where NumericalOrder gives 0:
strictNumericalOrder[e1_, e2_] := With[{no = NumericalOrder[e1, e2]}, If[no == 0, Order[e1, e2], no]]Sort[list, strictNumericalOrder]% === Sort[plist, strictNumericalOrder]Related Guides
History
Text
Wolfram Research (2017), NumericalOrder, Wolfram Language function, https://reference.wolfram.com/language/ref/NumericalOrder.html.
CMS
Wolfram Language. 2017. "NumericalOrder." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NumericalOrder.html.
APA
Wolfram Language. (2017). NumericalOrder. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NumericalOrder.html
BibTeX
@misc{reference.wolfram_2026_numericalorder, author="Wolfram Research", title="{NumericalOrder}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/NumericalOrder.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_numericalorder, organization={Wolfram Research}, title={NumericalOrder}, year={2017}, url={https://reference.wolfram.com/language/ref/NumericalOrder.html}, note=[Accessed: 13-June-2026]}