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

前端 未结 7 1168
情话喂你
情话喂你 2021-01-12 22:46

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

Code:

let timestamps = [\"2017-         


        
7条回答
  •  囚心锁ツ
    2021-01-12 23:24

    The strings are sortable using built-in sorting - no need for lodash or a sort function

    According to https://jsperf.com/array-sort-reverse-vs-string-comparison this is 1.2% slower than a string comparison - unimportant if few dates

    Sort and reverse:

    console.log(
    ["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" ]
     .sort()
     .reverse()
    )

提交回复
热议问题