Interface Comparator<T>

Represents a generic comparator function for comparing two elements of the same type.

Since

version 1.4.0

Author

Manuel Santos ney.br.santos@gmail.com

interface Comparator<T> {
    compare(a, b): number;
}

Type Parameters

  • T

    The type of elements to compare.

Methods

Methods

  • Compares two elements of type T and returns a number indicating their relative order.

    • If a is less than b, a negative number is returned.
    • If a is greater than b, a positive number is returned.
    • If a is equal to b, 0 is returned.

    Parameters

    • a: T

      The first element to compare.

    • b: T

      The second element to compare.

    Returns number

    A negative, positive, or zero value indicating the comparison result.

    Example

    const comparator: Comparator<number> = { compare: (a, b) => a - b };
    console.log(comparator.compare(3, 5)); // Output: -2

    Example

    const stringComparator: Comparator<string> = { compare: (a, b) => a.localeCompare(b) };
    console.log(stringComparator.compare("apple", "banana")); // Output: -1

Generated using TypeDoc