Adding the ability to _.sortBy(list, 'property')

This commit is contained in:
Yi, EungJun
2012-03-31 02:27:49 +09:00
parent 00ed5d70c8
commit 6b8a99ba39
2 changed files with 6 additions and 1 deletions

View File

@@ -235,6 +235,10 @@ $(document).ready(function() {
var people = [{name : 'curly', age : 50}, {name : 'moe', age : 30}];
people = _.sortBy(people, function(person){ return person.age; });
equal(_.pluck(people, 'name').join(', '), 'moe, curly', 'stooges sorted by age');
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,