【问题标题】:How to sort an array of timestamps using lodash in desc order如何使用 lodash 按 desc 顺序对时间戳数组进行排序
【发布时间】:2018-04-05 13:40:36
【问题描述】:

我想使用 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);

这不像我预期的那样工作,我相信它对它们进行排序,但按升序排列。

【问题讨论】:

  • ["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() - 不需要 lodash
  • 您是否有一个具有给定 iso 日期的数组,或者您是否有一个具有时间戳属性的对象数组?
  • @mplungjan 这在 asc 而不是 desc 中的作用相同。我需要从高到低
  • @Kay,我想你正在寻找_.orderBy() 而不是_.sortBy()
  • @Kay 然后改成arr.sort( (a, b) => b > a ? 1 : -1 )

标签: javascript lodash


【解决方案1】:

orderBy 允许您指定排序顺序,而 sortBy 不允许。

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

【讨论】:

    【解决方案2】:

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

    此代码已使用 lodash 正确排序时间戳:

    const sorted = _.sortBy(timestamps);
    

    只是按升序,只需使用以下命令反转结果:

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

    【讨论】:

      【解决方案3】:

      最简单的方法是使用 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)

      【讨论】:

      【解决方案4】:

      您可以将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() 可以正常工作 - 我发布了答案
      【解决方案5】:

      字符串可以使用内置排序进行排序 - 不需要 lodash 或排序函数

      根据https://jsperf.com/array-sort-reverse-vs-string-comparison,这比字符串比较慢 1.2% - 如果日期很少,则不重要

      Sortreverse

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

        【讨论】:

          【解决方案7】:

          使用这些选项可以使用另一种方法

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

          【讨论】:

            猜你喜欢
            • 2014-08-21
            • 1970-01-01
            • 1970-01-01
            • 2020-01-01
            • 2011-09-20
            • 1970-01-01
            • 2021-12-13
            • 2012-11-30
            • 2012-02-18
            相关资源
            最近更新 更多