| 1 | /**
|
|---|
| 2 | * Sorts an array of objects based on the given `criteria`.
|
|---|
| 3 | *
|
|---|
| 4 | * - If you provide keys, it sorts the objects by the values of those keys.
|
|---|
| 5 | * - If you provide functions, it sorts based on the values returned by those functions.
|
|---|
| 6 | *
|
|---|
| 7 | * The function returns the array of objects sorted in ascending order.
|
|---|
| 8 | * If two objects have the same value for the current criterion, it uses the next criterion to determine their order.
|
|---|
| 9 | *
|
|---|
| 10 | * @template T - The type of the objects in the array.
|
|---|
| 11 | * @param {T[]} arr - The array of objects to be sorted.
|
|---|
| 12 | * @param {Array<((item: T) => unknown) | keyof T>} criteria - The criteria for sorting. This can be an array of object keys or functions that return values used for sorting.
|
|---|
| 13 | * @returns {T[]} - The sorted array.
|
|---|
| 14 | *
|
|---|
| 15 | * @example
|
|---|
| 16 | * const users = [
|
|---|
| 17 | * { user: 'foo', age: 24 },
|
|---|
| 18 | * { user: 'bar', age: 7 },
|
|---|
| 19 | * { user: 'foo', age: 8 },
|
|---|
| 20 | * { user: 'bar', age: 29 },
|
|---|
| 21 | * ];
|
|---|
| 22 | *
|
|---|
| 23 | * sortBy(users, ['user', 'age']);
|
|---|
| 24 | * sortBy(users, [obj => obj.user, 'age']);
|
|---|
| 25 | * // results will be:
|
|---|
| 26 | * // [
|
|---|
| 27 | * // { user : 'bar', age: 7 },
|
|---|
| 28 | * // { user : 'bar', age: 29 },
|
|---|
| 29 | * // { user : 'foo', age: 8 },
|
|---|
| 30 | * // { user : 'foo', age: 24 },
|
|---|
| 31 | * // ]
|
|---|
| 32 | */
|
|---|
| 33 | declare function sortBy<T extends object>(arr: readonly T[], criteria: Array<((item: T) => unknown) | keyof T>): T[];
|
|---|
| 34 |
|
|---|
| 35 | export { sortBy };
|
|---|