_.sortBy

_.sortBy(collection, [iteratees=[_.identity]])

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. collection (Array|Object): The collection to iterate over.
  2. [iteratees=[_.identity]] (...(Function|Function[])): The iteratees to sort by.

Returns

(Array): Returns the new sorted array.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var users = [
  'user''fred',   'age': 48 },
 
  'user''barney''age': 36 },
 
  'user''fred',   'age': 40 },
 
  'user''barney''age': 34 }
];
  
_.sortBy(users, [function(o) { return o.user; }]);
// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
  
_.sortBy(users, ['user''age']);
// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]
doc_Lodash
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.