My current use case involves working with a table that contains dates and empty strings. Despite expecting the sorting to behave a certain way, I've encountered an issue.
Normally, when sorting with strings, the empty string would typically be placed at the top or bottom of the sorted list. However, with dates, it seems to push them in between other values.
json = ['01-01-2018', '', '04-03-2018', '03-03-2018', '', '04-03-2018', '03-03-2018'];
this.json.sort((obj1: string, obj2: string) => {
if (new Date(obj1) > new Date(obj2)) { return 1; }
if (new Date(obj1) < new Date(obj2)) { return -1; }
return 0;
});
As a result, the sorted list looks like this:
01-01-2018,,03-03-2018,04-03-2018,,03-03-2018,04-03-2018
.
For a demonstration, you can check out this Stackblitz example:
I have attempted converting the values to numbers for sorting, but the issue with the empty strings persists. Does anyone have a solution to address this problem?