merging #532 -- allow string as iterator on sortBy

This commit is contained in:
Jeremy Ashkenas
2012-04-02 13:46:49 -04:00
2 changed files with 6 additions and 1 deletions

View File

@@ -238,6 +238,10 @@ $(document).ready(function() {
var list = [undefined, 4, 1, undefined, 3, 2];
equal(_.sortBy(list, _.identity).join(','), '1,2,3,4,,', 'sortBy with undefined values');
var list = ["one", "two", "three", "four", "five"];
var sorted = _.sortBy(list, 'length');
equal(sorted.join(' '), 'one two four five three', 'sorted by length');
});
test('collections: groupBy', function() {

View File

@@ -262,7 +262,8 @@
};
// Sort the object's values by a criterion produced by an iterator.
_.sortBy = function(obj, iterator, context) {
_.sortBy = function(obj, val, context) {
var iterator = _.isFunction(val) ? val : function(obj) { return obj[val]; };
return _.pluck(_.map(obj, function(value, index, list) {
return {
value : value,