Function invertComparator

  • This function inverts the order of comparison of a sort-comparator function.

    Type Parameters

    • T

      The type of the elements that the comparator function compares.

    Parameters

    • comparator: ((a, b) => number)

      A comparator function that takes two arguments of type T and returns a number. The comparator function should return a negative number if the first argument is less than the second, zero if they are equal, and a positive number if the first argument is greater than the second.

        • (a, b): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns ((a, b) => number)

    • A new comparator function that takes two arguments of type T and returns a number. This function will return a negative number if the first argument is greater than the second, zero if they are equal, and a positive number if the first argument is less than the second.
      • (a, b): number
      • Parameters

        • a: T
        • b: T

        Returns number

    Example

    const numbers = [1, 2, 3, 4, 5];
    const comparator = (a, b) => a - b;
    const invertedComparator = invertComparator(comparator);
    numbers.sort(invertedComparator); // [5, 4, 3, 2, 1]

Generated using TypeDoc