Make _.sortByOrder support orders of "asc" and "desc".

This commit is contained in:
jdalton
2015-06-04 21:14:26 -07:00
parent 9e1f68d9eb
commit 56f199bd69
2 changed files with 26 additions and 21 deletions

View File

@@ -416,8 +416,8 @@
* sort them in ascending order.
*
* @private
* @param {Object} object The object to compare to `other`.
* @param {Object} other The object to compare to `object`.
* @param {Object} object The object to compare.
* @param {Object} other The other object to compare.
* @returns {number} Returns the sort order indicator for `object`.
*/
function compareAscending(object, other) {
@@ -425,16 +425,16 @@
}
/**
* Used by `_.sortByOrder` to compare multiple properties of each element
* in a collection and stable sort them in the following order:
* Used by `_.sortByOrder` to compare multiple properties of a value to another
* and stable sort them.
*
* If `orders` is unspecified, sort in ascending order for all properties.
* Otherwise, for each property, sort in ascending order if its corresponding value in
* orders is true, and descending order if false.
* If `orders` is unspecified, all valuess are sorted in ascending order. Otherwise,
* a value is sorted in ascending order if its corresponding order is "asc", and
* descending if "desc".
*
* @private
* @param {Object} object The object to compare to `other`.
* @param {Object} other The object to compare to `object`.
* @param {Object} object The object to compare.
* @param {Object} other The other object to compare.
* @param {boolean[]} orders The order to sort by for each property.
* @returns {number} Returns the sort order indicator for `object`.
*/
@@ -451,7 +451,8 @@
if (index >= ordersLength) {
return result;
}
return result * (orders[index] ? 1 : -1);
var order = orders[index];
return result * ((order === 'asc' || order === true) ? 1 : -1);
}
}
// Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
@@ -7263,9 +7264,9 @@
/**
* This method is like `_.sortByAll` except that it allows specifying the
* sort orders of the iteratees to sort by. A truthy value in `orders` will
* sort the corresponding property name in ascending order while a falsey
* value will sort it in descending order.
* sort orders of the iteratees to sort by. If `orders` is unspecified, all
* values are sorted in ascending order. Otherwise, a value is sorted in
* ascending order if its corresponding order is "asc", and descending if "desc".
*
* If a property name is provided for an iteratee the created `_.property`
* style callback returns the property value of the given element.
@@ -7279,7 +7280,7 @@
* @category Collection
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by.
* @param {boolean[]} orders The sort orders of `iteratees`.
* @param {boolean[]} [orders] The sort orders of `iteratees`.
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
* @returns {Array} Returns the new sorted array.
* @example
@@ -7292,7 +7293,7 @@
* ];
*
* // sort by `user` in ascending order and by `age` in descending order
* _.map(_.sortByOrder(users, ['user', 'age'], [true, false]), _.values);
* _.map(_.sortByOrder(users, ['user', 'age'], ['asc', 'desc']), _.values);
* // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
*/
function sortByOrder(collection, iteratees, orders, guard) {

View File

@@ -14525,14 +14525,18 @@
{ 'a': 'y', 'b': 2 }
];
test('should sort multiple properties by specified orders', 1, function() {
var actual = _.sortByOrder(objects, ['a', 'b'], [false, true]);
deepEqual(actual, [objects[3], objects[1], objects[2], objects[0]]);
test('should sort multiple properties by specified orders', 2, function() {
_.each([[false, true], ['desc', 'asc']], function(orders) {
var actual = _.sortByOrder(objects, ['a', 'b'], orders);
deepEqual(actual, [objects[3], objects[1], objects[2], objects[0]]);
});
});
test('should sort a property in ascending order when its order is not specified', 1, function() {
var actual = _.sortByOrder(objects, ['a', 'b'], [false]);
deepEqual(actual, [objects[3], objects[1], objects[2], objects[0]]);
test('should sort a property in ascending order when its order is not specified', 2, function() {
_.each([[false], ['desc']], function(orders) {
var actual = _.sortByOrder(objects, ['a', 'b'], orders);
deepEqual(actual, [objects[3], objects[1], objects[2], objects[0]]);
});
});
}());