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 allclose all

Basic Examples  (4)

These two numbers are not ordered:

These two are numerically the same:

Compare numeric expressions:

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

Compare quantities:

Compare dates:

Scope  (6)

Compare any two numeric expressions:

- comes before any real-valued expression:

comes after any real-valued expression:

Complex valued expressions are ordered first by the real part:

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

Compare quantities of compatible units:

The comparison is performed by converting into a common unit:

DateObject expressions are ordered by AbsoluteTime:

Use NumericalOrder as ordering function:

Sort using the ordering permutation:

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

Applications  (1)

Get the numerical ordering for a list:

Properties & Relations  (8)

For numeric expressions of different value, NumericalOrder compares them using those values:

Order always compares expressions structurally and may give different results:

Like Order, NumericalOrder is an antisymmetric function of expressions: NumericalOrder[e1,e2]==-NumericalOrder[e2,e1]:

Unlike Order, NumericalOrder[e1,e2] may return zero for non-identical e1, e2:

For comparable expressions e1, e2 a result NumericalOrder[e1,e2]0 implies e1-e2==0:

NumericalOrder compares inexact numbers using all available significant digits:

For machine-precision numbers, Less, Equal, Greater, etc. use 7 bits of tolerance:

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

NumericalOrder compares complex values by the real part and then by absolute value of the imaginary part:

This is consistent with Order for numbers:

Less, LessEqual and related functions cannot compare complex numbers:

Equivalent quantities have a NumericalOrder of 0:

The canonical order distinguishes between the two representations:

Use Equal to show that they are indeed equivalent quantities:

For non-numerical expressions e1, e2, NumericalOrder coincides with Order:

Possible Issues  (1)

Sorting with NumericalOrder will not guarantee a particular ordering for different representations of the same number:

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

The canonical order will rearrange in a definite way:

A stricter order can be defined by using Order to resolve cases where NumericalOrder gives 0:

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

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_2024_numericalorder, author="Wolfram Research", title="{NumericalOrder}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/NumericalOrder.html}", note=[Accessed: 24-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_numericalorder, organization={Wolfram Research}, title={NumericalOrder}, year={2017}, url={https://reference.wolfram.com/language/ref/NumericalOrder.html}, note=[Accessed: 24-November-2024 ]}