按时间戳对数组进行排序的最快方法

发布于 2024-12-06 07:06:34 字数 966 浏览 1 评论 0原文

我如何按时间戳对该数组进行排序以及最快的方法是什么(数组有很多条目)?

我的数组

  myList = new Array();

  myList[0]               = {};
  myList[0]['title']      = 'I am really new';
  myList[0]['timestamp']  = 1317039046;
  myList[0]['date']       = '2011-09-26T12:10:46+00:00'; 

  myList[1]               = {};
  myList[1]['title']      = 'I am the oldest';
  myList[1]['timestamp']  = 1315656646;
  myList[1]['date']       = '2011-09-10T12:10:46+00:00';

  myList[2]               = {};
  myList[2]['title']      = 'I am older';
  myList[2]['timestamp']  = 1316866246;
  myList[2]['date']       = '2011-09-24T12:10:46+00:00';

  myList[3]               = {};
  myList[3]['title']      = 'I am old';
  myList[3]['timestamp']  = 1316952646;
  myList[3]['date']       = '2011-09-25T12:10:46+00:00';

示例
http://jsbin.com/ejagup/2/edit#preview

how can i sort this array by timestamp and what is the fastest way (array has many many entries)?

my array

  myList = new Array();

  myList[0]               = {};
  myList[0]['title']      = 'I am really new';
  myList[0]['timestamp']  = 1317039046;
  myList[0]['date']       = '2011-09-26T12:10:46+00:00'; 

  myList[1]               = {};
  myList[1]['title']      = 'I am the oldest';
  myList[1]['timestamp']  = 1315656646;
  myList[1]['date']       = '2011-09-10T12:10:46+00:00';

  myList[2]               = {};
  myList[2]['title']      = 'I am older';
  myList[2]['timestamp']  = 1316866246;
  myList[2]['date']       = '2011-09-24T12:10:46+00:00';

  myList[3]               = {};
  myList[3]['title']      = 'I am old';
  myList[3]['timestamp']  = 1316952646;
  myList[3]['date']       = '2011-09-25T12:10:46+00:00';

example
http://jsbin.com/ejagup/2/edit#preview

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-12-13 07:06:34
myList.sort(function(x, y){
    return x.timestamp - y.timestamp;
})

myList 是一个 JavaScript 数组,支持 sort 方法。此方法接受一个函数作为参数,该函数根据返回值对数组进行排序。

目前,排序算法会将时间戳最小的元素放在前面。如果您想以另一个方向对数组进行排序,请交换 x.timestamp 和 y.timestamp 。

myList.sort(function(x, y){
    return x.timestamp - y.timestamp;
})

myList is a JavaScript array, which supports the sort method. This method accepts a function as argument, which sorts the array according to the returned value.

Currently, the sort algorithm will place the element with the lowest timestamp first. Swap x.timestamp and y.timestamp if you want to sort the array in the other direction.

偏爱自由 2024-12-13 07:06:34

在 JavaScript 中按日期对数组进行排序的最简单方法,
下面是 JavaScript 中按日期对数组进行排序的方法的示例

 const fetchMessageData = () => {

        return (
            AllMessagesArr.sort((x, y) => {
                return new Date(x.timestamp) < new Date(y.timestamp) ? 1 : -1
            })
        ).reverse()
    }

         console.log("Display messages in Ascending order" ,fetchMessageData())  

根据时间戳对数组进行排序

The easiest way to sort an array by date in JavaScript,
Here is an example of how this method of sorting an array by date in JavaScript looks

 const fetchMessageData = () => {

        return (
            AllMessagesArr.sort((x, y) => {
                return new Date(x.timestamp) < new Date(y.timestamp) ? 1 : -1
            })
        ).reverse()
    }

         console.log("Display messages in Ascending order" ,fetchMessageData())  

Sort the array on basis of timestamp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文