1 var baseCompareAscending = require('./baseCompareAscending');
4 * Used by `_.sortByOrder` to compare multiple properties of a value to another
5 * and stable sort them.
7 * If `orders` is unspecified, all valuess are sorted in ascending order. Otherwise,
8 * a value is sorted in ascending order if its corresponding order is "asc", and
9 * descending if "desc".
12 * @param {Object} object The object to compare.
13 * @param {Object} other The other object to compare.
14 * @param {boolean[]} 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 = baseCompareAscending(objCriteria[index], othCriteria[index]);
27 if (index >= ordersLength) {
30 var order = orders[index];
31 return result * ((order === 'asc' || order === true) ? 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://code.google.com/p/v8/issues/detail?id=90 for more details.
41 return object.index - other.index;
44 module.exports = compareMultiple;