简体   繁体   English

如何根据日期对对象数组进行排序

[英]How to sort an array of objects based on date

I have an array of objects which I am attempting to sort based on the createDate . 我有一个对象数组,我试图根据createDate进行排序。

[{
    "TestName": "com.DPProgram",
    "Test": {
        "createDate": "2018-02-15T17:17:10.000+0530",
        "effectiveStartDate": "1900-01-01T00:00:00.000+0530",
        "effectiveEndDate": "2200-01-01T00:00:00.000+0530"
    }
}, {
    "TestName": "com.DPProgram",
    "Test": {
        "createDate": "2018-02-22T15:00:11.000+0530",
        "effectiveStartDate": "2017-12-22T00:00:00.000+0530",
        "effectiveEndDate": "2018-12-23T00:00:00.000+0530"
    }
}];

data = data.sort(function(a, b) {
  data = data.sort(function(a, b) {
    return (a[data.createDate] > b[data.createDate]) 
  });

However it's not sorting 然而,它没有排序

https://jsfiddle.net/o2gxgz9r/51545/ https://jsfiddle.net/o2gxgz9r/51545/

You are comparing the dates as strings. 您将日期作为字符串进行比较。 You need to instead convert them to Date objects before making the comparison. 在进行比较之前,您需要将它们转换为Date对象。

Also your syntax of a[data.bean.createDate] is broken given the example. 此示例a[data.bean.createDate]破坏a[data.bean.createDate]语法。 You need to access obj.Test.createDate instead. 您需要访问obj.Test.createDate

Finally, don't use alert() for debugging, and especially not for any datatype more complex than a string. 最后,不要使用alert()进行调试,尤其是不要使用比字符串更复杂的任何数据类型。 console.log() is more accurate as it doesn't coerce data types, and lets you traverse through the levels or objects/arrays. console.log()更准确,因为它不强制数据类型,并允许您遍历级别或对象/数组。

With all that said, try this: 尽管如此,试试这个:

 var data = [{ "TestName": "com.DPProgram", "Test": { "createDate": "2018-02-15T17:17:10.000+0530", "effectiveStartDate": "1900-01-01T00:00:00.000+0530", "effectiveEndDate": "2200-01-01T00:00:00.000+0530" } }, { "TestName": "com.callidus.quotaDP.Tests.DPProgram", "Test": { "createDate": "2018-02-22T15:00:11.000+0530", "effectiveStartDate": "2017-12-22T00:00:00.000+0530", "effectiveEndDate": "2018-12-23T00:00:00.000+0530" } }, { "TestName": "com.Foo", "Test": { "createDate": "2018-02-07T15:00:11.000+0530", "effectiveStartDate": "2017-12-22T00:00:00.000+0530", "effectiveEndDate": "2018-12-23T00:00:00.000+0530" } }]; data = data.sort(function(a, b) { var aDate = new Date(a.Test.createDate), bDate = new Date(b.Test.createDate); return aDate > bDate ? 1 : aDate < bDate ? -1 : 0; }); console.log(data); 

 var data = [{ "TestName": "com.DPProgram", "Test": { "createDate": "2018-02-15T17:17:10.000+0530", "effectiveStartDate": "1900-01-01T00:00:00.000+0530", "effectiveEndDate": "2200-01-01T00:00:00.000+0530" } }, { "TestName": "com.DPProgram", "Test": { "createDate": "2018-02-22T15:00:11.000+0530", "effectiveStartDate": "2017-12-22T00:00:00.000+0530", "effectiveEndDate": "2018-12-23T00:00:00.000+0530" } },{ "TestName": "com.DPProgram", "Test": { "createDate": "2018-02-25T15:00:11.000+0530", "effectiveStartDate": "2017-12-22T00:00:00.000+0530", "effectiveEndDate": "2018-12-23T00:00:00.000+0530" } }]; console.log(_.sortBy(data, 'createDate')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore.js"></script> 

When you use a sort function, you get two arguments that are elements of the data array you are operating on, and so you need to use them: 当您使用排序函数时,您将获得两个参数,这两个参数是您正在操作的数据数组的元素,因此您需要使用它们:

data.sort(function(a, b) {
    return a.Test.createDate - b.Test.createDate;
});

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

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