matician - v2.0.0
    Preparing search index...

    Function sortBy

    • Sorts an array of objects by a specified property key.

      Type Parameters

      • T extends Record<string, any>

        Type of the items in the array.

      Parameters

      • array: T[]

        The array to sort.

      • key: keyof T

        The key to sort by.

      • direction: "asc" | "desc" = "asc"

        'asc' (default) or 'desc' for descending order.

      Returns T[]

      A new array sorted by the specified key.

      sortBy([{a: 3}, {a: 1}, {a: 2}], 'a'); // [{a: 1}, {a: 2}, {a: 3}]
      sortBy([{a: 'b'}, {a: 'a'}], 'a', 'desc'); // [{a: 'b'}, {a: 'a'}]