orderBy

collection

This method is like sortBy except that it allows specifying the sort orders of the iteratees. If orders is unspecified, all values are sorted in ascending order.

Signature

orderBy<T>(collection: T[], iteratees: (((value: T) => any) | keyof T)[], orders?: ('asc' | 'desc')[]): T[]

Parameters

NameTypeDescription
collectionT[]The collection to iterate over
iteratees(((value: T) => any) | keyof T)[]The iteratees to sort by
orders?('asc' | 'desc')[]The sort orders of iteratees

Returns

T[] - Returns the new sorted array

Examples

Multi-field sort

import { orderBy } from 'dashlite'

const users = [
  { name: 'fred', age: 48 },
  { name: 'barney', age: 34 },
  { name: 'fred', age: 40 }
]
const result = orderBy(users, ['name', 'age'], ['asc', 'desc'])
console.log(result)

Output:

[{ name: 'barney', ... }, { name: 'fred', age: 48 }, { name: 'fred', age: 40 }]

Descending order

import { orderBy } from 'dashlite'

const result = orderBy([3, 1, 4, 1, 5], [(n) => n], ['desc'])
console.log(result)

Output:

[5, 4, 3, 1, 1]

Available since version 1.2.0