mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Allow _.sortBy to accept an array for callback.
This commit is contained in:
67
lodash.js
67
lodash.js
@@ -280,22 +280,29 @@
|
||||
*/
|
||||
function compareAscending(a, b) {
|
||||
var ac = a.criteria,
|
||||
bc = b.criteria;
|
||||
bc = b.criteria,
|
||||
index = -1,
|
||||
length = ac.length;
|
||||
|
||||
// ensure a stable sort in V8 and other engines
|
||||
// http://code.google.com/p/v8/issues/detail?id=90
|
||||
if (ac !== bc) {
|
||||
if (ac > bc || typeof ac == 'undefined') {
|
||||
return 1;
|
||||
}
|
||||
if (ac < bc || typeof bc == 'undefined') {
|
||||
return -1;
|
||||
while (++index < length) {
|
||||
var value = ac[index],
|
||||
other = bc[index];
|
||||
|
||||
if (value !== other) {
|
||||
if (value > other || typeof value == 'undefined') {
|
||||
return 1;
|
||||
}
|
||||
if (value < other || typeof other == 'undefined') {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// The JS engine embedded in Adobe applications like InDesign has a buggy
|
||||
// `Array#sort` implementation that causes it, under certain circumstances,
|
||||
// to return the same value for `a` and `b`.
|
||||
// See https://github.com/jashkenas/underscore/pull/1247
|
||||
// Fixes an `Array#sort` bug in the JS engine embedded in Adobe applications
|
||||
// that causes it, under certain circumstances, to return the same value for
|
||||
// `a` and `b`. See https://github.com/jashkenas/underscore/pull/1247
|
||||
//
|
||||
// This also ensures a stable sort in V8 and other engines.
|
||||
// See http://code.google.com/p/v8/issues/detail?id=90
|
||||
return a.index - b.index;
|
||||
}
|
||||
|
||||
@@ -4319,6 +4326,9 @@
|
||||
* If a property name is provided for `callback` the created "_.pluck" style
|
||||
* callback will return the property value of the given element.
|
||||
*
|
||||
* If an array of property names is provided for `callback` the collection
|
||||
* will be sorted by each property value.
|
||||
*
|
||||
* If an object is provided for `callback` the created "_.where" style callback
|
||||
* will return `true` for elements that have the properties of the given object,
|
||||
* else `false`.
|
||||
@@ -4327,7 +4337,7 @@
|
||||
* @memberOf _
|
||||
* @category Collections
|
||||
* @param {Array|Object|string} collection The collection to iterate over.
|
||||
* @param {Function|Object|string} [callback=identity] The function called
|
||||
* @param {Array|Function|Object|string} [callback=identity] The function called
|
||||
* per iteration. If a property name or object is provided it will be used
|
||||
* to create a "_.pluck" or "_.where" style callback, respectively.
|
||||
* @param {*} [thisArg] The `this` binding of `callback`.
|
||||
@@ -4340,19 +4350,37 @@
|
||||
* _.sortBy([1, 2, 3], function(num) { return this.sin(num); }, Math);
|
||||
* // => [3, 1, 2]
|
||||
*
|
||||
* var characters = [
|
||||
* { 'name': 'barney', 'age': 36 },
|
||||
* { 'name': 'fred', 'age': 40 },
|
||||
* { 'name': 'barney', 'age': 26 },
|
||||
* { 'name': 'fred', 'age': 30 }
|
||||
* ];
|
||||
*
|
||||
* // using "_.pluck" callback shorthand
|
||||
* _.sortBy(['banana', 'strawberry', 'apple'], 'length');
|
||||
* // => ['apple', 'banana', 'strawberry']
|
||||
* _.map(_.sortBy(characters, 'age'), _.values);
|
||||
* // => [['barney', 26], ['fred', 30], ['barney', 36], ['fred', 40]]
|
||||
*
|
||||
* // sorting by multiple properties
|
||||
* _.map(_.sortBy(characters, ['name', 'age']), _.values);
|
||||
* // = > [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
|
||||
*/
|
||||
function sortBy(collection, callback, thisArg) {
|
||||
var index = -1,
|
||||
isArr = isArray(callback),
|
||||
length = collection ? collection.length : 0,
|
||||
result = Array(typeof length == 'number' ? length : 0);
|
||||
|
||||
callback = lodash.createCallback(callback, thisArg, 3);
|
||||
if (!isArr) {
|
||||
callback = lodash.createCallback(callback, thisArg, 3);
|
||||
}
|
||||
forEach(collection, function(value, key, collection) {
|
||||
var object = result[++index] = getObject();
|
||||
object.criteria = callback(value, key, collection);
|
||||
if (isArr) {
|
||||
object.criteria = map(callback, function(key) { return value[key]; });
|
||||
} else {
|
||||
(object.criteria = getArray())[0] = callback(value, key, collection);
|
||||
}
|
||||
object.index = index;
|
||||
object.value = value;
|
||||
});
|
||||
@@ -4362,6 +4390,9 @@
|
||||
while (length--) {
|
||||
var object = result[length];
|
||||
result[length] = object.value;
|
||||
if (!isArr) {
|
||||
releaseArray(object.criteria);
|
||||
}
|
||||
releaseObject(object);
|
||||
}
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user