f1b64f7a29847da271628d7c67ad078303687ca8
[yaffs-website] / node_modules / grunt-legacy-log-utils / node_modules / lodash / orderBy.js
1 var baseOrderBy = require('./_baseOrderBy'),
2     isArray = require('./isArray');
3
4 /**
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.
9  *
10  * @static
11  * @memberOf _
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.
18  * @example
19  *
20  * var users = [
21  *   { 'user': 'fred',   'age': 48 },
22  *   { 'user': 'barney', 'age': 34 },
23  *   { 'user': 'fred',   'age': 42 },
24  *   { 'user': 'barney', 'age': 36 }
25  * ];
26  *
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]]
30  */
31 function orderBy(collection, iteratees, orders, guard) {
32   if (collection == null) {
33     return [];
34   }
35   if (!isArray(iteratees)) {
36     iteratees = iteratees == null ? [] : [iteratees];
37   }
38   orders = guard ? undefined : orders;
39   if (!isArray(orders)) {
40     orders = orders == null ? [] : [orders];
41   }
42   return baseOrderBy(collection, iteratees, orders);
43 }
44
45 module.exports = orderBy;