1 | import arrayMap from './_arrayMap.js';
|
---|
2 | import baseGet from './_baseGet.js';
|
---|
3 | import baseIteratee from './_baseIteratee.js';
|
---|
4 | import baseMap from './_baseMap.js';
|
---|
5 | import baseSortBy from './_baseSortBy.js';
|
---|
6 | import baseUnary from './_baseUnary.js';
|
---|
7 | import compareMultiple from './_compareMultiple.js';
|
---|
8 | import identity from './identity.js';
|
---|
9 | import isArray from './isArray.js';
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * The base implementation of `_.orderBy` without param guards.
|
---|
13 | *
|
---|
14 | * @private
|
---|
15 | * @param {Array|Object} collection The collection to iterate over.
|
---|
16 | * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
|
---|
17 | * @param {string[]} orders The sort orders of `iteratees`.
|
---|
18 | * @returns {Array} Returns the new sorted array.
|
---|
19 | */
|
---|
20 | function baseOrderBy(collection, iteratees, orders) {
|
---|
21 | if (iteratees.length) {
|
---|
22 | iteratees = arrayMap(iteratees, function(iteratee) {
|
---|
23 | if (isArray(iteratee)) {
|
---|
24 | return function(value) {
|
---|
25 | return baseGet(value, iteratee.length === 1 ? iteratee[0] : iteratee);
|
---|
26 | }
|
---|
27 | }
|
---|
28 | return iteratee;
|
---|
29 | });
|
---|
30 | } else {
|
---|
31 | iteratees = [identity];
|
---|
32 | }
|
---|
33 |
|
---|
34 | var index = -1;
|
---|
35 | iteratees = arrayMap(iteratees, baseUnary(baseIteratee));
|
---|
36 |
|
---|
37 | var result = baseMap(collection, function(value, key, collection) {
|
---|
38 | var criteria = arrayMap(iteratees, function(iteratee) {
|
---|
39 | return iteratee(value);
|
---|
40 | });
|
---|
41 | return { 'criteria': criteria, 'index': ++index, 'value': value };
|
---|
42 | });
|
---|
43 |
|
---|
44 | return baseSortBy(result, function(object, other) {
|
---|
45 | return compareMultiple(object, other, orders);
|
---|
46 | });
|
---|
47 | }
|
---|
48 |
|
---|
49 | export default baseOrderBy;
|
---|