javascript-collections-framework
    Preparing search index...

    Function naturalOrder

    • Returns the comparator for natural order.

      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.

      Type Parameters

      • T

      Returns Comparator<T>

      A comparator for natural order.

      NullElementError (from the comparator) when a value is null or undefined.

      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.

      TypeError (from the comparator) when a [compareSymbol] method returns a non-number or NaN.

      [10, 9, 1].sort(naturalOrder()) // [1, 9, 10] — native sort without a comparator gives [1, 10, 9]