javascript-collections-framework
    Preparing search index...

    Function nullsFirst

    • Returns a comparator that puts null and undefined before every other value, treats them as equal to each other, and orders the rest with comparator (natural order when omitted). comparator is never called with null or undefined.

      Native Array.prototype.sort always moves undefined and empty slots to the end without calling the comparator, so with native sort this moves only null to the front.

      Time: O(1) plus at most one call of comparator per comparison.

      Type Parameters

      • T

      Parameters

      • Optionalcomparator: Comparator<T>

        Orders the values that are not null or undefined.

      Returns Comparator<T | null | undefined>

      The comparator.

      TypeError when comparator is given but is not a function (including null).

      NullElementError, ClassCastError or TypeError (from the comparator, when natural order is used) as described for naturalOrder.

      [3, null, 1].sort(nullsFirst()) // [null, 1, 3]