Function sortObjectByMultipleKeys

  • Sorts an array of objects based on multiple keys.

    Parameters

    • arr: AnyObject[] = []

      The array of objects to be sorted.

    • keys: string[] = []

      The keys to sort the objects. Prefix a key with a hyphen (-) to sort in descending order.

    Returns AnyObject[]

    The sorted array of objects.

    Example

    const object = [
    { name: 'John', date: '2021-01-01' },
    { name: 'Alice', date: '2021-02-01' },
    { name: 'Bob', date: '2021-01-15' }
    ];

    const sortedArray = sortObjectByMultipleKeys(object, ['name', '-date']);
    console.log(sortedArray);
    // Output: [
    // { name: 'Alice', date: '2021-02-01' },
    // { name: 'Bob', date: '2021-01-15' },
    // { name: 'John', date: '2021-01-01' }
    // ]

Generated using TypeDoc