source: node_modules/es-toolkit/dist/compat/array/sortBy.d.mts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 3.0 KB
Line 
1import { ListIteratee } from '../_internal/ListIteratee.mjs';
2import { Many } from '../_internal/Many.mjs';
3import { ObjectIteratee } from '../_internal/ObjectIteratee.mjs';
4
5/**
6 * Sorts an array of objects based on multiple properties and their corresponding order directions.
7 *
8 * This function takes an array of objects, an array of criteria to sort by.
9 * It returns the ascending sorted array, ordering by each key.
10 * If values for a key are equal, it moves to the next key to determine the order.
11 *
12 * @template T - The type of elements in the array.
13 * @param {ArrayLike<T> | object | null | undefined} collection - The array of objects to be sorted.
14 * @param {Array<Array<Criterion<T> | Criterion<T>>>} criteria - An array of criteria (property names or property paths or custom key functions) to sort by.
15 * @returns {T[]} - The ascending 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 * const result = sortBy(users, ['user', (item) => item.age])
26 * // result will be:
27 * // [
28 * // { user: 'barney', age: 34 },
29 * // { user: 'barney', age: 36 },
30 * // { user: 'fred', age: 40 },
31 * // { user: 'fred', age: 48 },
32 * // ]
33 */
34/**
35 * Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee.
36 *
37 * @template T
38 * @param {ArrayLike<T> | null | undefined} collection - The collection to iterate over.
39 * @param {...Array<T | readonly T[] | ListIteratee<T>>} iteratees - The iteratees to sort by.
40 * @returns {T[]} Returns the new sorted array.
41 *
42 * @example
43 * const users = [
44 * { 'user': 'fred', 'age': 48 },
45 * { 'user': 'barney', 'age': 36 },
46 * { 'user': 'fred', 'age': 42 },
47 * { 'user': 'barney', 'age': 34 }
48 * ];
49 *
50 * sortBy(users, [function(o) { return o.user; }]);
51 * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
52 */
53declare function sortBy<T>(collection: ArrayLike<T> | null | undefined, ...iteratees: Array<Many<ListIteratee<T>>>): T[];
54/**
55 * Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru each iteratee.
56 *
57 * @template T
58 * @param {T | null | undefined} collection - The object to iterate over.
59 * @param {...Array<T[keyof T] | readonly Array<T[keyof T]> | ObjectIteratee<T>>} iteratees - The iteratees to sort by.
60 * @returns {Array<T[keyof T]>} Returns the new sorted array.
61 *
62 * @example
63 * const users = {
64 * 'a': { 'user': 'fred', 'age': 48 },
65 * 'b': { 'user': 'barney', 'age': 36 }
66 * };
67 *
68 * sortBy(users, [function(o) { return o.user; }]);
69 * // => [{ 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 48 }]
70 */
71declare function sortBy<T extends object>(collection: T | null | undefined, ...iteratees: Array<Many<ObjectIteratee<T>>>): Array<T[keyof T]>;
72
73export { sortBy };
Note: See TracBrowser for help on using the repository browser.