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

末鹿安然 提交于 2020-01-21 08:11:47

问题


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.


回答1:


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



回答2:


orderBy allows you to specify the sort orders while sortBy does not.

const sorted = orderBy(timestamps, ['desc']);



回答3:


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)



回答4:


You could treat ISO 8601 date strings as normal strings, because they are sortable as string.

var array = ["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"];

array.sort((a, b) => b > a || -(b < a));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }



回答5:


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



回答6:


Another method could be possible with this options

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 sorterFunc = item => (new Date() - new Date(item));
const sorted = _.sortBy(timestamps, [sorterFunc]);

those will sort the array without having to call a method .reverse() at all.




回答7:


var users = ["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"];

_.sortBy(users, String);


来源:https://stackoverflow.com/questions/49673861/how-to-sort-an-array-of-timestamps-using-lodash-in-desc-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!