1 | import compareAscending from './_compareAscending.js';
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * Used by `_.orderBy` to compare multiple properties of a value to another
|
---|
5 | * and stable sort them.
|
---|
6 | *
|
---|
7 | * If `orders` is unspecified, all values are sorted in ascending order. Otherwise,
|
---|
8 | * specify an order of "desc" for descending or "asc" for ascending sort order
|
---|
9 | * of corresponding values.
|
---|
10 | *
|
---|
11 | * @private
|
---|
12 | * @param {Object} object The object to compare.
|
---|
13 | * @param {Object} other The other object to compare.
|
---|
14 | * @param {boolean[]|string[]} orders The order to sort by for each property.
|
---|
15 | * @returns {number} Returns the sort order indicator for `object`.
|
---|
16 | */
|
---|
17 | function compareMultiple(object, other, orders) {
|
---|
18 | var index = -1,
|
---|
19 | objCriteria = object.criteria,
|
---|
20 | othCriteria = other.criteria,
|
---|
21 | length = objCriteria.length,
|
---|
22 | ordersLength = orders.length;
|
---|
23 |
|
---|
24 | while (++index < length) {
|
---|
25 | var result = compareAscending(objCriteria[index], othCriteria[index]);
|
---|
26 | if (result) {
|
---|
27 | if (index >= ordersLength) {
|
---|
28 | return result;
|
---|
29 | }
|
---|
30 | var order = orders[index];
|
---|
31 | return result * (order == 'desc' ? -1 : 1);
|
---|
32 | }
|
---|
33 | }
|
---|
34 | // Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
|
---|
35 | // that causes it, under certain circumstances, to provide the same value for
|
---|
36 | // `object` and `other`. See https://github.com/jashkenas/underscore/pull/1247
|
---|
37 | // for more details.
|
---|
38 | //
|
---|
39 | // This also ensures a stable sort in V8 and other engines.
|
---|
40 | // See https://bugs.chromium.org/p/v8/issues/detail?id=90 for more details.
|
---|
41 | return object.index - other.index;
|
---|
42 | }
|
---|
43 |
|
---|
44 | export default compareMultiple;
|
---|