Reputation: 718
I've read the previous question on this topic: Lodash : how to do a case insensitive sorting on a collection using orderBy?
I found that lodash correctly sorts numeric and date columns. I need case-insensitive sorting for the string columns. We currently have the following implementation:
const sorted = _.orderBy(this, function (o) {
if ($.isNumeric(o[column])) {
return parseFloat(o[column]);
}
return (o[column]).toLowerCase();
}, direction);
this.clear();
for (let i = 0; i < sorted.length; i++) {
this.push(sorted[i]);
}
My concern is that implementation is not going to handle dates properly. Do we need anything special here for dates?
Upvotes: 0
Views: 262
Reputation: 14927
_.sortBy
can sort dates - assuming all the values in the column are dates, you only need to test the type and return the date:
const sorted = _.orderBy(this, function(o) {
if ($.isNumeric(o[column])) {
return parseFloat(o[column]);
}
if (_.isDate(o[column])) {
return o[column];
}
return (o[column]).toLowerCase();
}, direction);
Upvotes: 1