1 var baseFlatten = require('./_baseFlatten'),
2 baseOrderBy = require('./_baseOrderBy'),
3 baseRest = require('./_baseRest'),
4 isIterateeCall = require('./_isIterateeCall');
7 * Creates an array of elements, sorted in ascending order by the results of
8 * running each element in a collection thru each iteratee. This method
9 * performs a stable sort, that is, it preserves the original sort order of
10 * equal elements. The iteratees are invoked with one argument: (value).
15 * @category Collection
16 * @param {Array|Object} collection The collection to iterate over.
17 * @param {...(Function|Function[])} [iteratees=[_.identity]]
18 * The iteratees to sort by.
19 * @returns {Array} Returns the new sorted array.
23 * { 'user': 'fred', 'age': 48 },
24 * { 'user': 'barney', 'age': 36 },
25 * { 'user': 'fred', 'age': 40 },
26 * { 'user': 'barney', 'age': 34 }
29 * _.sortBy(users, [function(o) { return o.user; }]);
30 * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
32 * _.sortBy(users, ['user', 'age']);
33 * // => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]
35 var sortBy = baseRest(function(collection, iteratees) {
36 if (collection == null) {
39 var length = iteratees.length;
40 if (length > 1 && isIterateeCall(collection, iteratees[0], iteratees[1])) {
42 } else if (length > 2 && isIterateeCall(iteratees[0], iteratees[1], iteratees[2])) {
43 iteratees = [iteratees[0]];
45 return baseOrderBy(collection, baseFlatten(iteratees, 1), []);
48 module.exports = sortBy;