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 | * @since 4.0.0
|
---|
13 | * @category Collection
|
---|
14 | * @param {Array|Object} collection The collection to iterate over.
|
---|
15 | * @param {Array[]|Function[]|Object[]|string[]} [iteratees=[_.identity]]
|
---|
16 | * The iteratees to sort by.
|
---|
17 | * @param {string[]} [orders] The sort orders of `iteratees`.
|
---|
18 | * @param- {Object} [guard] Enables use as an iteratee for methods like `_.reduce`.
|
---|
19 | * @returns {Array} Returns the new sorted array.
|
---|
20 | * @example
|
---|
21 | *
|
---|
22 | * var users = [
|
---|
23 | * { 'user': 'fred', 'age': 48 },
|
---|
24 | * { 'user': 'barney', 'age': 34 },
|
---|
25 | * { 'user': 'fred', 'age': 40 },
|
---|
26 | * { 'user': 'barney', 'age': 36 }
|
---|
27 | * ];
|
---|
28 | *
|
---|
29 | * // Sort by `user` in ascending order and by `age` in descending order.
|
---|
30 | * _.orderBy(users, ['user', 'age'], ['asc', 'desc']);
|
---|
31 | * // => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]
|
---|
32 | */
|
---|
33 | function orderBy(collection, iteratees, orders, guard) {
|
---|
34 | if (collection == null) {
|
---|
35 | return [];
|
---|
36 | }
|
---|
37 | if (!isArray(iteratees)) {
|
---|
38 | iteratees = iteratees == null ? [] : [iteratees];
|
---|
39 | }
|
---|
40 | orders = guard ? undefined : orders;
|
---|
41 | if (!isArray(orders)) {
|
---|
42 | orders = orders == null ? [] : [orders];
|
---|
43 | }
|
---|
44 | return baseOrderBy(collection, iteratees, orders);
|
---|
45 | }
|
---|
46 |
|
---|
47 | module.exports = orderBy;
|
---|