Search code examples
javascriptlodash

How to sort an array of timestamps using lodash in desc order


I want to sort the following array of timestamps using lodash so the latest time stamp is first [3].

Code:

let timestamps = ["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"]

const sorted = _.sortBy(timestamps);

This does not work as i expect, i believe its sorting them but in asc order.


Solution

  • 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();