Sometimes, we want to fix sort by date not working with TypeScript.
In this article, we’ll look at how to fix sort by date not working with TypeScript.
How to fix sort by date not working with TypeScript?
To fix sort by date not working with TypeScript, we can use the difference in the timestamps between 2 entries in the array.
For instance, we write
const sorted = myArray.sort((a: TaskItemVO, b: TaskItemVO) => {
return a.dueDate.getTime() - b.dueDate.getTime();
});
to call myArray.sort
with a callback that returns the difference of the dueDate
timestamps which we get from the getTime
method.
The sort
callback should return a number and subtracting timestamps would return a number since timestamps are numbers.
Conclusion
To fix sort by date not working with TypeScript, we can use the difference in the timestamps between 2 entries in the array.