Function sortByCb

  • Creates sort callback which can be used for array.sort input. A custom key function can be supplied to customize the sort order. Key can return any nested data structure, supports number, booleans, arrays.

    Type Parameters

    • T

    Parameters

    • key: ((item) => unknown) = ...

      a function to execute to decide the order

        • (item): unknown
        • Parameters

          • item: T

          Returns unknown

    Returns ((a, b) => number)

    array.sort compatible callback

      • (a, b): number
      • Parameters

        Returns number

    Example

    // Sort by object key
    arr.sort(sortByCb(v => v.myKey))
    // Sort by number in reversed order
    arr.sort(sortByCb(v => v.numberKey * -1))
    // Sort by object key, then by boolean value in revered order
    arr.sort(sortByCb(v => [v.myKey, !v.boolValue])