| 1 | /**
|
|---|
| 2 | * Sorts an array of objects based on the given `criteria` and their corresponding order directions.
|
|---|
| 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 corresponding order directions.
|
|---|
| 8 | * If two objects have the same value for the current criterion, it uses the next criterion to determine their order.
|
|---|
| 9 | * If the number of orders is less than the number of criteria, it uses the last order for the rest of the criteria.
|
|---|
| 10 | *
|
|---|
| 11 | * @template T - The type of elements in the array.
|
|---|
| 12 | * @param {T[]} arr - The array of objects to be sorted.
|
|---|
| 13 | * @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.
|
|---|
| 14 | * @param {Array<'asc' | 'desc'>} orders - An array of order directions ('asc' for ascending or 'desc' for descending).
|
|---|
| 15 | * @returns {T[]} - The sorted array.
|
|---|
| 16 | *
|
|---|
| 17 | * @example
|
|---|
| 18 | * // Sort an array of objects by 'user' in ascending order and 'age' in descending order.
|
|---|
| 19 | * const users = [
|
|---|
| 20 | * { user: 'fred', age: 48 },
|
|---|
| 21 | * { user: 'barney', age: 34 },
|
|---|
| 22 | * { user: 'fred', age: 40 },
|
|---|
| 23 | * { user: 'barney', age: 36 },
|
|---|
| 24 | * ];
|
|---|
| 25 | *
|
|---|
| 26 | * const result = orderBy(users, [obj => obj.user, 'age'], ['asc', 'desc']);
|
|---|
| 27 | * // result will be:
|
|---|
| 28 | * // [
|
|---|
| 29 | * // { user: 'barney', age: 36 },
|
|---|
| 30 | * // { user: 'barney', age: 34 },
|
|---|
| 31 | * // { user: 'fred', age: 48 },
|
|---|
| 32 | * // { user: 'fred', age: 40 },
|
|---|
| 33 | * // ]
|
|---|
| 34 | */
|
|---|
| 35 | declare function orderBy<T extends object>(arr: readonly T[], criteria: Array<((item: T) => unknown) | keyof T>, orders: Array<'asc' | 'desc'>): T[];
|
|---|
| 36 |
|
|---|
| 37 | export { orderBy };
|
|---|