Skip to content

Commit 938d2d3

Browse files
committed
Handle undefined values in sortBy for Underbar.
1 parent 0c11aee commit 938d2d3

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/underbar.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,28 @@ var _ = {};
590590
_.sortBy = function(collection, iterator) {
591591
//apply iterator to collection
592592

593+
//sort by key
593594
if (typeof iterator==='string') {
594595
return collection.sort(function(a, b) {
595-
return (a[iterator]-b[iterator]);
596+
if (a[iterator]===undefined) {
597+
return 1;
598+
} else {
599+
return (a[iterator]-b[iterator]);
600+
}
596601
});
597-
} else {
602+
} else { //sort by function
598603
var iterated=[];
599604
var newCollection=[];
600605
_.each(collection, function(val) {
601606
iterated.push([val, iterator(val)]);
602607
});
603608

604609
iterated.sort(function(a, b) {
605-
return a[1]-b[1];
610+
if (a[1]===undefined) {
611+
return 1;
612+
} else {
613+
return a[1]-b[1];
614+
}
606615
});
607616
_.each(iterated, function(val) {
608617
newCollection.push(val[0]);

0 commit comments

Comments
 (0)