sortBy
collectionCreates an array of elements, sorted in ascending order by the results of running each element through each iteratee. This method performs a stable sort.
Signature
sortBy<T>(collection: T[], iteratees: (((value: T) => any) | keyof T)[]): T[]Parameters
| Name | Type | Description |
|---|---|---|
collection | T[] | The collection to iterate over |
iteratees | (((value: T) => any) | keyof T)[] | The iteratees to sort by |
Returns
T[] - Returns the new sorted array
Examples
Sort by property
import { sortBy } from 'dashlite'
const users = [
{ name: 'fred', age: 48 },
{ name: 'barney', age: 36 },
{ name: 'fred', age: 30 }
]
const result = sortBy(users, ['name', 'age'])
console.log(result)Output:
[{ name: 'barney', age: 36 }, { name: 'fred', age: 30 }, { name: 'fred', age: 48 }]Sort by function
import { sortBy } from 'dashlite'
const result = sortBy([{ n: 3 }, { n: 1 }, { n: 2 }], [(o) => o.n])
console.log(result)Output:
[{ n: 1 }, { n: 2 }, { n: 3 }]Available since version 1.2.0