按时间戳对数组进行排序的最快方法
我如何按时间戳对该数组进行排序以及最快的方法是什么(数组有很多条目)?
我的数组
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';
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';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
myList
是一个 JavaScript 数组,支持sort
方法。此方法接受一个函数作为参数,该函数根据返回值对数组进行排序。目前,排序算法会将时间戳最小的元素放在前面。如果您想以另一个方向对数组进行排序,请交换 x.timestamp 和 y.timestamp 。
myList
is a JavaScript array, which supports thesort
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
andy.timestamp
if you want to sort the array in the other direction.在 JavaScript 中按日期对数组进行排序的最简单方法,
下面是 JavaScript 中按日期对数组进行排序的方法的示例
根据时间戳对数组进行排序
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
Sort the array on basis of timestamp