简体   繁体   English

如何按日期对Javascript对象数组进行排序?

[英]How to Sort Javascript Object Array By Date?

I have a problem to sort my object arrays by Date values. 我在按日期值对对象数组进行排序时遇到问题。 I found many questions that other users asked before, I went through all possible solutions and I still did not get solution for my sorting problem. 我找到了其他用户之前问过的很多问题,我经历了所有可能的解决方案,但仍然没有得到解决我的排序问题的解决方案。 I created two different arrays to test sort function too but still could not get dates in correct order. 我也创建了两个不同的数组来测试排序功能,但是仍然无法以正确的顺序获取日期。 Here is my data: 这是我的数据:

[Object { eventDate="12/12/2016",  display_order="5"},Object { eventDate="12/12/2016",  display_order="3"}]

[Object { eventDate="04/21/2016",  display_order="3"},Object { eventDate="04/21/2016",  display_order="1"}]

I used this logic to sort my data by date values: 我使用此逻辑按日期值对数据进行排序:

entries.sort(function(a,b){
    var i = new Date(a.eventDate);
    var j = new Date(b.eventDate);
    return i-j;
});

My data still did not sort in correct order using this function. 使用此功能,我的数据仍然没有按照正确的顺序排序。 Then I also tried to sort my array that looks like this: 然后,我还尝试对我的数组进行排序,如下所示:

var dates = Object.keys(groupedByDate);

console.log(dates)

This is output for variable dates: ["12/12/2016", "04/21/2016"] 输出的是可变日期: ["12/12/2016", "04/21/2016"]

Then I tried to do this: 然后我尝试这样做:

dates.sort(function(a, b) {
    a = new Date(a.dates);
    b = new Date(b.dates);
    return a>b ? -1 : a<b ? 1 : 0;
});

My output still did not sort my dates in correct order. 我的输出仍然没有按正确的顺序对日期排序。 I'm not sure am I doing something wrong or something else could be a problem in this case. 我不确定我做错了什么,否则在这种情况下可能会出问题。 If anyone can help please let me know. 如果有人可以帮忙,请告诉我。 Thanks. 谢谢。

Sort dates using Date.getTime() function ( by number of milliseconds ): 使用Date.getTime()函数对日期排序( 按毫秒数 ):

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

You could split the date and sort with the parts. 您可以分割日期并按部分排序。

 var array = [{ eventDate: "12/12/2016", display_order: "5" }, { eventDate: "12/12/2016", display_order: "3" }, { eventDate: "04/21/2016", display_order: "3" }, { eventDate: "04/21/2016", display_order: "1" }]; array.sort(function (a, b) { var aa = a.eventDate.split('/'), bb = b.eventDate.split('/'); return aa[2] - bb[2] || aa[0] - bb[0] || aa[1] - bb[1]; }); document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>'); 

Seems there is an issue in the json you pasted. 似乎您粘贴的json中存在问题。 { eventDate="12/12/2016", display_order="3"} .Instead of = it has to be : Assuming you have correct json format the below solutions can be useful { eventDate="12/12/2016", display_order="3"}不是=必须是:假设您具有正确的json格式,以下解决方案可能会有用

var array = [{ eventDate:"12/12/2016",  display_order:"5"}, 
                { eventDate:"12/12/2016",  display_order:"3"},
                { eventDate:"04/21/2016",  display_order:"3"},
                { eventDate:"04/21/2016",  display_order:"1"}];

// Generic solution
var sortedArray = array.sort(function(a,b) { 
    return new Date(a.eventDate).getTime() - new Date(b.eventDate).getTime() 
});


// Solution using ternary operator
var l =array.sort(function(a,b){
  return a.eventDate>b.eventDate ? -1 : a.eventDate<b.eventDate ? 1 : 0;
});

Demo 演示

The answers here are on the right track but you should never use the Date constructor, or Date.parse, to parse strings. 答案在正确的轨道上,但是您绝对不应使用Date构造函数或Date.parse来解析字符串。 Use a library and pass the format, or write a simple function, eg 使用一个库并传递格式,或编写一个简单的函数,例如

 function parseMDY(s) { var b = s.split('/'); return new Date(b[2],b[0]-1,b[1]); } document.write(parseMDY('4/30/2016')); 

So now you have: 现在,您有了:

entries.sort(function(a,b) {
  return parseMDY(a) - parseMDY(b);
});

So I tried all answers above, some of them gave me close but not fully solution for my case. 因此,我尝试了上面的所有答案,其中有些给了我接近但无法完全解决的情况。 So I just tried to combine and this is correct solution for my problem: 所以我只是想结合起来,这是解决我的问题的正确方法:

dates.sort(function(a, b) {
    return new Date(a).getTime() - new Date(b).getTime();
});

If I tried to return just A and B, that worked for one record, but then when I switch user in my onChange drop down I was getting reverse dates again. 如果我只尝试返回A和B,那么该记录就起作用了,但是当我在onChange下拉列表中切换用户时,我又得到了反向日期。 So solution above with new Date() and getTime() works fine. 因此,使用new Date()getTime()上述解决方案可以正常工作。 Thank you all for your help! 谢谢大家的帮助!

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

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