Skip to main content

orderBy

_.orderBy(iteratees, orders, collection) # Ⓢ [Ⓣ][1] Ⓝ

This method is like _.sortBy except that it allows specifying the sort



orders of the iteratees to sort by. If orders is unspecified, all values



are sorted in ascending order. Otherwise, specify an order of "desc" for



descending or "asc" for ascending sort order of corresponding values.

Since​

4.0.0

Arguments​

  1. iteratees (Array[]|Function[]|Object[]|string[]): The iteratees to sort by.
  2. orders (string[]): The sort orders of iteratees.
  3. collection (Array|Object): The collection to iterate over.

Returns​

(Array): Returns the new sorted array.

Example​

var users = [
{ 'user': 'fred', 'age': 48 },
{ 'user': 'barney', 'age': 34 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'barney', 'age': 36 }
];

// Sort by `user` in ascending order and by `age` in descending order.
_.orderBy(['user', 'age'], ['asc', 'desc'], users);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]