1 var baseOrderBy = require('./_baseOrderBy'),
2 isArray = require('./isArray');
5 * This method is like `_.sortBy` except that it allows specifying the sort
6 * orders of the iteratees to sort by. If `orders` is unspecified, all values
7 * are sorted in ascending order. Otherwise, specify an order of "desc" for
8 * descending or "asc" for ascending sort order of corresponding values.
12 * @category Collection
13 * @param {Array|Object} collection The collection to iterate over.
14 * @param {Function[]|Object[]|string[]} [iteratees=[_.identity]] The iteratees to sort by.
15 * @param {string[]} [orders] The sort orders of `iteratees`.
16 * @param- {Object} [guard] Enables use as an iteratee for functions like `_.reduce`.
17 * @returns {Array} Returns the new sorted array.
21 * { 'user': 'fred', 'age': 48 },
22 * { 'user': 'barney', 'age': 34 },
23 * { 'user': 'fred', 'age': 42 },
24 * { 'user': 'barney', 'age': 36 }
27 * // Sort by `user` in ascending order and by `age` in descending order.
28 * _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
29 * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
31 function orderBy(collection, iteratees, orders, guard) {
32 if (collection == null) {
35 if (!isArray(iteratees)) {
36 iteratees = iteratees == null ? [] : [iteratees];
38 orders = guard ? undefined : orders;
39 if (!isArray(orders)) {
40 orders = orders == null ? [] : [orders];
42 return baseOrderBy(collection, iteratees, orders);
45 module.exports = orderBy;