-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathsortByAll.js
53 lines (49 loc) · 1.78 KB
/
sortByAll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import baseEach from '../internal/baseEach';
import baseFlatten from '../internal/baseFlatten';
import baseSortBy from '../internal/baseSortBy';
import compareMultipleAscending from '../internal/compareMultipleAscending';
import isIterateeCall from '../internal/isIterateeCall';
import isLength from '../internal/isLength';
/**
* This method is like `_.sortBy` except that it sorts by property names
* instead of an iteratee function.
*
* @static
* @memberOf _
* @category Collection
* @param {Array|Object|string} collection The collection to iterate over.
* @param {...(string|string[])} props The property names to sort by,
* specified as individual property names or arrays of property names.
* @returns {Array} Returns the new sorted array.
* @example
*
* var users = [
* { 'user': 'barney', 'age': 36 },
* { 'user': 'fred', 'age': 40 },
* { 'user': 'barney', 'age': 26 },
* { 'user': 'fred', 'age': 30 }
* ];
*
* _.map(_.sortByAll(users, ['user', 'age']), _.values);
* // => [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
*/
function sortByAll(collection) {
var args = arguments;
if (args.length > 3 && isIterateeCall(args[1], args[2], args[3])) {
args = [collection, args[1]];
}
var index = -1,
length = collection ? collection.length : 0,
props = baseFlatten(args, false, false, 1),
result = isLength(length) ? Array(length) : [];
baseEach(collection, function(value, key, collection) {
var length = props.length,
criteria = Array(length);
while (length--) {
criteria[length] = value == null ? undefined : value[props[length]];
}
result[++index] = { 'criteria': criteria, 'index': index, 'value': value };
});
return baseSortBy(result, compareMultipleAscending);
}
export default sortByAll;