1 var baseSortByOrder = require('../internal/baseSortByOrder'),
2 isArray = require('../lang/isArray'),
3 isIterateeCall = require('../internal/isIterateeCall');
6 * This method is like `_.sortByAll` except that it allows specifying the
7 * sort orders of the iteratees to sort by. If `orders` is unspecified, all
8 * values are sorted in ascending order. Otherwise, a value is sorted in
9 * ascending order if its corresponding order is "asc", and descending if "desc".
11 * If a property name is provided for an iteratee the created `_.property`
12 * style callback returns the property value of the given element.
14 * If an object is provided for an iteratee the created `_.matches` style
15 * callback returns `true` for elements that have the properties of the given
16 * object, else `false`.
20 * @category Collection
21 * @param {Array|Object|string} collection The collection to iterate over.
22 * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
23 * @param {boolean[]} [orders] The sort orders of `iteratees`.
24 * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
25 * @returns {Array} Returns the new sorted array.
29 * { 'user': 'fred', 'age': 48 },
30 * { 'user': 'barney', 'age': 34 },
31 * { 'user': 'fred', 'age': 42 },
32 * { 'user': 'barney', 'age': 36 }
35 * // sort by `user` in ascending order and by `age` in descending order
36 * _.map(_.sortByOrder(users, ['user', 'age'], ['asc', 'desc']), _.values);
37 * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
39 function sortByOrder(collection, iteratees, orders, guard) {
40 if (collection == null) {
43 if (guard && isIterateeCall(iteratees, orders, guard)) {
46 if (!isArray(iteratees)) {
47 iteratees = iteratees == null ? [] : [iteratees];
49 if (!isArray(orders)) {
50 orders = orders == null ? [] : [orders];
52 return baseSortByOrder(collection, iteratees, orders);
55 module.exports = sortByOrder;