diff --git a/test/collections.js b/test/collections.js index 6ffa5b59f..7e5ec6185 100644 --- a/test/collections.js +++ b/test/collections.js @@ -503,6 +503,19 @@ deepEqual(_.sortBy(collection, 'x'), collection, 'sortBy accepts property string'); + var collectionWithDifferentDatatypes = [ + new Pair('1', 100), new Pair('1', 1), + new Pair('1', 2), new Pair('1', 200) + ]; + + var sortedList = _.sortBy(collectionWithDifferentDatatypes, function(pair) { + return [pair.x, pair.y]; + }); + + var expectedSortedList = [new Pair('1', 1), new Pair('1', 2), new Pair('1', 100), new Pair('1', 200)]; + + deepEqual(sortedList, expectedSortedList, 'sortBy should sort it'); + list = ['q', 'w', 'e', 'r', 't', 'y']; deepEqual(_.sortBy(list), ['e', 'q', 'r', 't', 'w', 'y'], 'uses _.identity if iterator is not specified'); }); diff --git a/underscore.js b/underscore.js index a455456d2..55a74e7b3 100644 --- a/underscore.js +++ b/underscore.js @@ -347,6 +347,10 @@ var a = left.criteria; var b = right.criteria; if (a !== b) { + for (var index = 0; index < _.keys(a).length; index++) { + if (a[index] > b[index] || a === void 0) return 1; + if (a[index] < b[index] || b === void 0) return -1; + } if (a > b || a === void 0) return 1; if (a < b || b === void 0) return -1; }