1 var compareAscending = require('./_compareAscending');
4 * Used by `_.orderBy` to compare multiple properties of a value to another
5 * and stable sort them.
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.
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`.
17 function compareMultiple(object, other, orders) {
19 objCriteria = object.criteria,
20 othCriteria = other.criteria,
21 length = objCriteria.length,
22 ordersLength = orders.length;
24 while (++index < length) {
25 var result = compareAscending(objCriteria[index], othCriteria[index]);
27 if (index >= ordersLength) {
30 var order = orders[index];
31 return result * (order == 'desc' ? -1 : 1);
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
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;
44 module.exports = compareMultiple;