2024-07-05 04:11:56 +03:00
# orderBy
2024-08-22 17:28:27 +03:00
Sorts an array of objects based on the given `criteria` and their corresponding order directions.
2024-07-05 04:11:56 +03:00
2024-08-22 17:28:27 +03:00
- If you provide keys, it sorts the objects by the values of those keys.
- If you provide functions, it sorts based on the values returned by those functions.
The function returns the array of objects sorted in corresponding order directions.
If two objects have the same value for the current criterion, it uses the next criterion to determine their order.
If the number of orders is less than the number of criteria, it uses the last order for the remaining criteria.
2024-07-05 04:11:56 +03:00
## Signature
```typescript
2024-08-22 17:28:27 +03:00
function orderBy< T extends object > (
arr: T[],
criteria: Array< ((item: T) => unknown) | keyof T>,
orders: Array< 'asc' | 'desc'>
): T[];
2024-07-05 04:11:56 +03:00
```
### Parameters
2024-08-22 17:28:27 +03:00
- `arr` (`T[]`): The array of objects to be sorted.
- `criteria` (`Array< keyof T | (( item: T ) = > unknown)>`): The criteria for sorting. This can be an array of object keys or functions that return values used for sorting.
- `orders` (`Array< 'asc' | 'desc'>)`): An array of order directions ('asc' for ascending or 'desc' for descending).
2024-07-05 04:11:56 +03:00
### Returns
(`T[]`) The sorted array.
## Examples
```typescript
2024-08-22 17:28:27 +03:00
// Sort an array of objects by 'user' in ascending order and 'age' in descending order.
2024-07-05 04:11:56 +03:00
const users = [
{ user: 'fred', age: 48 },
{ user: 'barney', age: 34 },
{ user: 'fred', age: 40 },
{ user: 'barney', age: 36 },
];
2024-08-22 17:28:27 +03:00
const result = orderBy(users, [obj => obj.user, 'age'], ['asc', 'desc']);
2024-07-05 04:11:56 +03:00
// result will be:
// [
// { user: 'barney', age: 36 },
// { user: 'barney', age: 34 },
// { user: 'fred', age: 48 },
// { user: 'fred', age: 40 },
// ]
```