Instead of forcing Date
objects into their millisecond-since-The-Epoch values, it's better to directly obtain the milliseconds-since-The-Epoch value and utilize it in the subtraction operation.
You haven't provided information about what type a.date
and b.date
are, but we can assume they might be strings, numbers, or perhaps even instances of Date
.
If a.date
and b.date
are strings
In the case where a.date
and b.date
are strings, you can leverage Date.parse
to parse the strings with rules similar to those of new Date
, allowing you to directly obtain the milliseconds-since-The-Epoch value:
return Date.parse(b.date) - Date.parse(a.date);
Please note that both this approach and the original code make an assumption that a.date
and b.date
are appropriately formatted to be parsed by the Date
object.
If a.date
and b.date
are numbers
In the scenario where a.date
and b.date
are already represented as milliseconds-since-The-Epoch values, you can simply use them directly:
return b.date - a.date;
If a.date
and b.date
are instances of Date
If a.date
and b.date
are instances of Date
, you can utilize the getTime
method to retrieve their underlying milliseconds-since-The-Epoch values:
return b.date.getTime() - a.date.getTime();