• Category

    Sort an array using multiple compare functions. Elements are sorted which each compare function in the order they were provided. If two elements are equal according to a compare function, the next compare function is used.

    Time complexity: O(n)

    Space complexity: O(n)

    Type Parameters

    • T

    Parameters

    • Rest...compareFns: readonly CompareFn<T>[]

      Compare functions to use

    Returns CompareFn<T>

    A compare function that combines the provided compare functions

    Sort

    const array = [
    {a: 1, b: 2},
    {a: 1, b: 1},
    {a: 2, b: 2},
    {a: 2, b: 1},
    ];

    array.sort(
    Sort.combine(
    Sort.descending(x => x.b),
    Sort.descending(x => x.a),
    ),
    );

    console.log(array);
    [
    {a: 2, b: 2},
    {a: 1, b: 2},
    {a: 2, b: 1},
    {a: 1, b: 1},
    ]