Function compareWith

  • Creates a comparator from an ordered list of comparators.

    The returned comparator calls each given comparator in order until one determines the values are not equal.

    Type Parameters

    Parameters

    • Rest...comparators: T

      used in order to compare values

    Returns MergeComparators<T>

    a new comparator that uses the given comparators

    const list = [
    { foo: "bbb", bar: "yyy" },
    { foo: "aaa", bar: "xxx" },
    { foo: "bbb", bar: "xxx" },
    ];
    // sorts the list comparing by "foo" and then by "bar"
    list.sort(
    compareWith(
    compareBy("foo"),
    compareBy("bar"),
    )
    );
    // Result:
    // [
    // { foo: "aaa", bar: "xxx" },
    // { foo: "bbb", bar: "xxx" },
    // { foo: "bbb", bar: "yyy" },
    // ]