Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseFlatten = require('../internal/baseFlatten'),
2
baseSortByOrder = require('../internal/baseSortByOrder'),
3
isIterateeCall = require('../internal/isIterateeCall'),
4
restParam = require('../function/restParam');
5
6
/**
7
* This method is like `_.sortBy` except that it can sort by multiple iteratees
8
* or property names.
9
*
10
* If a property name is provided for an iteratee the created `_.property`
11
* style callback returns the property value of the given element.
12
*
13
* If an object is provided for an iteratee the created `_.matches` style
14
* callback returns `true` for elements that have the properties of the given
15
* object, else `false`.
16
*
17
* @static
18
* @memberOf _
19
* @category Collection
20
* @param {Array|Object|string} collection The collection to iterate over.
21
* @param {...(Function|Function[]|Object|Object[]|string|string[])} iteratees
22
* The iteratees to sort by, specified as individual values or arrays of values.
23
* @returns {Array} Returns the new sorted array.
24
* @example
25
*
26
* var users = [
27
* { 'user': 'fred', 'age': 48 },
28
* { 'user': 'barney', 'age': 36 },
29
* { 'user': 'fred', 'age': 42 },
30
* { 'user': 'barney', 'age': 34 }
31
* ];
32
*
33
* _.map(_.sortByAll(users, ['user', 'age']), _.values);
34
* // => [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]
35
*
36
* _.map(_.sortByAll(users, 'user', function(chr) {
37
* return Math.floor(chr.age / 10);
38
* }), _.values);
39
* // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
40
*/
41
var sortByAll = restParam(function(collection, iteratees) {
42
if (collection == null) {
43
return [];
44
}
45
var guard = iteratees[2];
46
if (guard && isIterateeCall(iteratees[0], iteratees[1], guard)) {
47
iteratees.length = 1;
48
}
49
return baseSortByOrder(collection, baseFlatten(iteratees), []);
50
});
51
52
module.exports = sortByAll;
53
54