Skip to main content

sortBy

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

Creates an array of elements, sorted in ascending order by the results of



running each element in a collection thru each iteratee. This method



performs a stable sort, that is, it preserves the original sort order of



equal elements. The iteratees are invoked with one argument: (value).

Since​

0.1.0

Arguments​

  1. iteratees (Function|Function[]): The iteratees to sort by.
  2. 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': 36 },
{ 'user': 'fred', 'age': 30 },
{ 'user': 'barney', 'age': 34 }
];

_.sortBy([function(o) { return o.user; }], users);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 30]]

_.sortBy(['user', 'age'], users);
// => objects for [['barney', 34], ['barney', 36], ['fred', 30], ['fred', 48]]