1 var baseFlatten = require('./_baseFlatten'),
2 baseOrderBy = require('./_baseOrderBy'),
3 isIterateeCall = require('./_isIterateeCall'),
4 rest = require('./rest');
7 * Creates an array of elements, sorted in ascending order by the results of
8 * running each element in a collection through 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).
14 * @category Collection
15 * @param {Array|Object} collection The collection to iterate over.
16 * @param {...(Function|Function[]|Object|Object[]|string|string[])} [iteratees=[_.identity]]
17 * The iteratees to sort by, specified individually or in arrays.
18 * @returns {Array} Returns the new sorted array.
22 * { 'user': 'fred', 'age': 48 },
23 * { 'user': 'barney', 'age': 36 },
24 * { 'user': 'fred', 'age': 42 },
25 * { 'user': 'barney', 'age': 34 }
28 * _.sortBy(users, function(o) { return o.user; });
29 * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
31 * _.sortBy(users, ['user', 'age']);
32 * // => objects for [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]
34 * _.sortBy(users, 'user', function(o) {
35 * return Math.floor(o.age / 10);
37 * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
39 var sortBy = rest(function(collection, iteratees) {
40 if (collection == null) {
43 var length = iteratees.length;
44 if (length > 1 && isIterateeCall(collection, iteratees[0], iteratees[1])) {
46 } else if (length > 2 && isIterateeCall(iteratees[0], iteratees[1], iteratees[2])) {
49 return baseOrderBy(collection, baseFlatten(iteratees), []);
52 module.exports = sortBy;