I can't figure out why the sort function doesn't sort the array containing undefined.
var sortBy = function(collection, iterator) {
var newArr = map(collection, function(item, key, collection) {
if (item === undefined, null) {
return [undefined, undefined];
} else {
if (typeof(iterator) === 'string') {
return [item, item[iterator]];
} else {
var results = iterator(item);
return [item, results];
}
}
});
newArr.sort(function(a, b) {
return a[1] - b[1];
});
return map(newArr, function(item, key, collection) {
return item[0];
});
};
var list = [4, 1, undefined, 3, 2];
sortBy(list, function(i) { return i; });
If i remove the undefined, the array sorts just fine. With it, it doesn't sort at all. Thanks for your help! (PS I'm new to coding so any other tips/recs on this are welcome)
if(item === undefined, null){should beif(item === undefined || item === null){.