Function compareBy

  • Creates a comparator that compares two values of type T, based on the selected value of type O and the given comparator. If no comparator is passed, the naturalOrder comparator will be used.

    Example

    const idComparator = compareBy(x => x.id, naturalOrder);
    [{ id: 1, name: 'JavaScript' }, { id: 0, name: 'TypeScript' }].sort(idComparator);

    Type Parameters

    Type Parameters

    • T

    • O

    Parameters

    • selector: ((t: T) => O)

      function to extract the comparable value

        • (t: T): O
        • Parameters

          • t: T

          Returns O

    • comparator: Comparator<O> = naturalOrder

      comparator for the extracted values, defaults to naturalOrder

    Returns Comparator<T>

Generated using TypeDoc