How to sort an array of timestamps using lodash in desc order
orderBy allows you to specify the sort orders while sortBy does not.
const sorted = orderBy(timestamps, ['desc']);
How to sort an array of timestamps using lodash
This code is already sorting timestamps correctly using lodash:
const sorted = _.sortBy(timestamps);
just in ascending order, simply reverse the result using:
const sorted = _.sortBy(timestamps).reverse();
The simplest way is to use Array.prototype.sort instead of lodash.
Kudos to @mplungjan and @gurvinder372 for pointing out that new Date
is useless.
Keep in mind that Array.prototype.sort updates the array on the place.
const dates = [
"2017-01-15T19:18:13.000Z",
"2016-11-24T17:33:56.000Z",
"2017-04-24T00:41:18.000Z",
"2017-03-06T01:45:29.000Z",
"2017-03-05T03:30:40.000Z"
]
dates.sort((d1, d2) => d2 > d1 ? 1 : -1) // desc
console.log(dates)
dates.sort() // asc
console.log(dates)