diff --git a/test/collections.js b/test/collections.js index fb457f605..4f34ea4a8 100644 --- a/test/collections.js +++ b/test/collections.js @@ -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() { diff --git a/underscore.js b/underscore.js index e3db3646d..f1f816da8 100644 --- a/underscore.js +++ b/underscore.js @@ -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,