简体   繁体   English

使用java脚本对带有日期和时间的大量数组数据进行排序

[英]Sort large set of array data with date and time using java script

Have a large list of array data of which can exceed to N number.拥有大量的数组数据,其中可以超过 N 个。 Need to sort the array elements with latest date and time.需要使用最新的日期和时间对数组元素进行排序。 在此处输入图片说明

let array = [{"name": "Apple","modified_date": "2020-08-26 02:03:23.756"},
             {"name": "Orange","modified_date": "2020-08-26 03:03:23.756"},
             {"name": "Grapes","modified_date": "2020-08-26 04:03:23.756"}................N number length]

able to sort using small size of array能够使用小数组进行排序

let sort = array.sort(function(a, b) { 
            return (new Date(b.modified_date)).getTime() - (new Date(a.modified_date)).getTime();
        });

But with large length array correct sorting is not happening.What could be the issue!!!!?但是对于大长度数组,正确的排序不会发生。可能是什么问题!!!!?

You could avoid creation of O(2*n*log(n)) Date objects.您可以避免创建O(2*n*log(n)) Date对象。 String compare will yield the same result as Date(str).getTime() compare yields.字符串比较将产生与Date(str).getTime()比较产生相同的结果。 The string comparison method is a very much better idea.字符串比较方法是一个更好的主意。

 let array = [{"name": "Apple","modified_date": "2020-08-26 02:03:23.756"}, {"name": "Orange","modified_date": "2020-08-26 03:03:23.756"}, {"name": "Grapes","modified_date": "2020-08-26 04:03:23.756"}]; array.sort((a, b) => b.modified_date.localeCompare(a.modified_date)); console.log(array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM