Class Comparator<T>

A Comparator defines a compare function enriched with methods to compose multiple comparators in order to form complex comparison behavior. A compare function returns negative numbers if the first value is lower than the second value, positive numbers if the first value is larger than the second value and zero if both values are equal.

Type Parameters

  • T

Constructors

Properties

compare: ((a, b) => number)

Type declaration

    • (a, b): number
    • Parameters

      Returns number

Methods

  • Composes the current comparator with the given comparison function such that the latter is applied for every equal values of the former comparator.

    Parameters

    • nextComparison: ((a, b) => number)
        • (a, b): number
        • Parameters

          Returns number

    Returns Comparator<T>

  • Composes the current comparator with a comparator which compares the properties selected by the given selector function for every equal values of the current comparator.

    Parameters

    • selector: ((value) => any)
        • (value): any
        • Parameters

          • value: T

          Returns any

    Returns Comparator<T>

  • Composes the current comparator with a comparator which compares the values of the given key for every equal values of the current comparator.

    Parameters

    • key: keyof NonNullable<T>

    Returns Comparator<T>

  • Composes the current comparator with a comparator which compares the properties selected by the given selector function for every equal values of the current comparator in reverse (descending) order.

    Parameters

    • selector: ((value) => any)
        • (value): any
        • Parameters

          • value: T

          Returns any

    Returns Comparator<T>

  • Composes the current comparator with a comparator which compares the values of the given key for every equal values of the current comparator in reverse (descending) order.

    Parameters

    • key: keyof NonNullable<T>

    Returns Comparator<T>

  • Composes the current comparator with the given comparison function such that the latter is applied for every equal values of the current comparator in reverse (descending) order.

    Parameters

    • nextComparison: ((a, b) => number)
        • (a, b): number
        • Parameters

          Returns number

    Returns Comparator<T>

  • Type Parameters

    • T

    Parameters

    • a: T
    • b: T
    • Optional keyOrSelector: ((item) => any) | keyof NonNullable<T>

    Returns number