如何使用 lodash 按照时间戳降序对数组进行排序

7

我希望能使用 Lodash 对以下时间戳数组进行排序,使最新的时间戳排在第一位 [3]。

代码:

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

这不符合我的预期,我认为它正在排序,但是按升序排序。


5
["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" ] 按时间顺序排序。 - mplungjan
你是否有一个包含给定 ISO 日期的数组,或者你是否有一个包含时间戳属性的对象数组? - Nina Scholz
@mplungjan 这个是按升序而不是降序排列。我需要从高到低排序。 - Kay
@Kay,我认为你需要使用_.orderBy()而不是_.sortBy() - Alexander
1
@Kay 然后将其修改为 arr.sort( (a, b) => b > a ? 1 : -1 ) - gurvinder372
7个回答

9
如何使用lodash对时间戳数组进行排序
这段代码已经使用lodash正确地对时间戳进行了排序:
const sorted = _.sortBy(timestamps);

如果只是按升序排列,可以简单地使用以下方法来反转结果:

const sorted = _.sortBy(timestamps).reverse();

9

orderBy 可以让你指定排序顺序,而 sortBy 则不行。

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

2
最简单的方法是使用 Array.prototype.sort 而不是 lodash。 感谢 @mplungjan 和 @gurvinder372 指出 new Date 是无用的。
请注意,Array.prototype.sort 会在原地更新数组。

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)


1
为什么要创建日期?当字符串排序时,它们按日期顺序排列。 - mplungjan
2
@havenchyk 完全可行 arr.sort( (a, b) => b > a ? 1 : -1 ) - gurvinder372
for desc it's needed :) - uladzimir
@mplungjan 但是您需要对数组进行两次修改 - uladzimir
但是使用内置方法而不是比较函数——JSPerf可以告诉我们什么最快——啊——存在。https://jsperf.com/array-sort-reverse-vs-string-comparison - 看起来字符串比较快1.2%——当我们只有5个字符串时并不多。 - mplungjan
显示剩余2条评论

1

您可以将 ISO 8601日期字符串视为普通字符串,因为它们可以作为字符串进行排序。

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; }


不需要排序函数。.sort().reverse() 就可以很好地工作 - 我发布了一个答案。 - mplungjan

1
字符串可使用内置排序进行排序 - 无需使用lodash或sort函数。
根据https://jsperf.com/array-sort-reverse-vs-string-comparison,相对于字符串比较,这种方法慢了1.2% - 如果日期很少,则不重要。 排序反转

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


0

使用这些选项可能还有另一种方法

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

这些方法可以在不调用.reverse()方法的情况下对数组进行排序。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接