• Creates a sorting function for an array of objects based on a specified key.

    Type Parameters

    • T

      The type of the objects in the array.

    Parameters

    • key: keyof T

      The key of the object to base the sorting on. The key must have values of either type string or number.

    Returns (a: T, b: T) => number

    A comparator function to sort objects by the specified key. The comparator can be used in array sort operations. For numbers, it sorts in descending order. For strings, it sorts in ascending lexicographical order.

    Throws an error if the key's type is neither number nor string.