Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
4 changes: 4 additions & 0 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@
var a = left.criteria;
var b = right.criteria;
if (a !== b) {
for (var index = 0; index < _.keys(a).length; index++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will call _.keys(a) on each iteration, you would want to cache this.

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;
}
Expand Down