Numbers compare numerically (-0 equals 0; NaN comes after every other number), strings by
UTF-16 code units (the order of < on two strings, identical on every runtime; use
Intl.Collator().compare for a locale's order), bigints numerically and booleans with false
first, dates by time value (invalid dates last), and objects that implement [compareSymbol] by
that method. Values are never converted to strings or compared with < coercion.
Time: O(1) per comparison for numbers and booleans; O(1) amortised for dates and
[compareSymbol] objects (plus the method); O(length of the common prefix) for strings;
O(digits) for bigints.
NullElementError (from the comparator) when a value is null or undefined.
Throws
ClassCastError (from the comparator) when the values are of different kinds, such as a
number and a string, or a number and a bigint, or have no natural order.
Throws
TypeError (from the comparator) when a [compareSymbol] method returns a non-number or
NaN.
Example
[10, 9, 1].sort(naturalOrder()) // [1, 9, 10] — native sort without a comparator gives [1, 10, 9]
Returns the comparator for natural order.
Numbers compare numerically (
-0equals0;NaNcomes after every other number), strings by UTF-16 code units (the order of<on two strings, identical on every runtime; useIntl.Collator().comparefor a locale's order), bigints numerically and booleans withfalsefirst, dates by time value (invalid dates last), and objects that implement[compareSymbol]by that method. Values are never converted to strings or compared with<coercion.Time: O(1) per comparison for numbers and booleans; O(1) amortised for dates and
[compareSymbol]objects (plus the method); O(length of the common prefix) for strings; O(digits) for bigints.