简体   繁体   English

根据日期对一组JavaScript对象进行排序

[英]Sorting an array of JavaScript objects according to date

i have that file json: 我有那个文件json:

var Point = [{

"id": 1,
"name": "A",
"LastUpdate": "2016-07-08",
"position": [36.8479648, 10.2793332]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-07",
"position":[ 36.8791039, 10.2656209]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-09",
"position": [36.9922751, 10.1255164]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-10",
"position": [36.9009882, 10.3009531]},{
"id": 1,
"name": "A",
"LastUpdate": "2016-07-04",
"position": [37.2732415, 9.8713665]}];

How can i sort the objects by the LastUpdate(it's a date) property in ascending and descending order using only JavaScript? 如何仅使用JavaScript按LastUpdate(日期)属性按升序和降序对对象排序?

Try like this 这样尝试

Point.sort(function(a, b) {
  return (new Date(b.LastUpdate)) - (new Date(a.LastUpdate))
})

DEMO

As your date format is YYYY-MM-DD ( year, followed by month, followed by date ), you can simply compare them as strings using String's localeCompare() method. 由于您的日期格式为YYYY-MM-DD(年,后跟月份,后跟date),因此您可以使用String的localeCompare()方法将它们作为字符串进行比较。

Then, you just need to add this to your custom sort function as described below: 然后,只需将其添加到自定义排序函数中,如下所述:

 var Points = [{ "id": 1, "name": "A", "LastUpdate": "2016-07-08", "position": [36.8479648, 10.2793332]},{ "id": 1, "name": "A", "LastUpdate": "2016-07-07", "position":[ 36.8791039, 10.2656209]},{ "id": 1, "name": "A", "LastUpdate": "2016-07-09", "position": [36.9922751, 10.1255164]},{ "id": 1, "name": "A", "LastUpdate": "2016-07-10", "position": [36.9009882, 10.3009531]},{ "id": 1, "name": "A", "LastUpdate": "2016-07-04", "position": [37.2732415, 9.8713665]}]; Points.sort(function(a, b){ return a.LastUpdate.localeCompare( b.LastUpdate ); }); console.log( Points ); 

As string are in ISO format, they can be sorted by direct string comparison, so no need to convert them in different type of object. 由于字符串采用ISO格式,因此可以通过直接字符串比较对其进行排序,因此无需将其转换为其他类型的对象。

As pointed by Nina in comments, chrome uses different sorting algorithm for arrays larger than 10, so after all the benchmark test, the best method to sort will be 正如Nina在评论中指出的那样,Chrome对大于10的数组使用了不同的排序算法,因此在进行所有基准测试之后,最好的排序方法是

1st in performance (Avg. ms per task 0.011690500000258907) 性能第一(每个任务的平均ms 0.011690500000258907)

Point.sort(function(a,b) {
return (a.LastUpdate > b.LastUpdate) ? 1 : ((b.LastUpdate > a.LastUpdate) ?-1:0);
});

Than 2nd in performance (Avg. ms per task 0.029657999999879395) 性能超过第二(每个任务的平均ms 0.029657999999879395)

Point.sort(function(a, b) {
  return a.LastUpdate.localeCompare( b.LastUpdate );
})

3rd in performance (Avg. ms per task 0.03225850000019418) 性能第三名(每个任务的平均毫秒数0.03225850000019418)

Point.sort(function(a, b) {
  return (new Date(b.LastUpdate)) - (new Date(a.LastUpdate))
})

You can use the sort function with a compare function. 您可以将排序功能与比较功能一起使用。

Point.sort(function(a, b) {
    var aSplit = a.LastUpdate.split("-"),
        aDate = new Date(aSplit[2], aSplit[1], aSplit[0]),
        bSplit = b.LastUpdate.split("-"),
        bDate = new Date(bSplit [2], bSplit [1], bSplit [0]);

    return aDate - bDate;
});

Simple solution using Array.sort (with ES6 "arrow" function) and Date.parse function: 使用Array.sort (带有ES6“箭头”功能)和Date.parse函数的简单解决方案:

// ascending order
Point.sort((a, b) => Date.parse(a.LastUpdate) - Date.parse(b.LastUpdate));

ascending and descending : 上升和下降:

ascPoint = Point.sort((a,b) => Date.parse(a.LastUpdate) -Date.parse(b.LastUpdate));
descPoint = ascPoint.reverse();

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

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