I have an array of classes and the dates they were offered coming back from an AJAX to PHP call. It's returned as 'data' so we'll just call our array data:
我有一个类的数组,并提供从AJAX到PHP调用的日期。它作为'数据'返回,所以我们只需调用我们的数组数据:
var data = [{
"course": "First Aid",
"courseDate": "2016-04-25T00:00:00-06:00"
}, {
"course": "CPR",
"courseDate": "2016-04-06T00:00:00-06:00"
}, {
"course": "ASL1",
"courseDate": "2016-01-07T00:00:00-06:00"
}, {
"course": "ASL2",
"courseDate": "2016-03-25T00:00:00-06:00"
},
...etc...
];
I need to be able to display them sorted by date descending. I'm using this simple function:
我需要能够按日期降序显示它们。我正在使用这个简单的功能:
data.sort(function(a, b) {
a = new Date(a.courseDate);
b = new Date(b.courseDate);
return a > b ? -1 : a < b ? 1 : 0;
});
$.each(data, function(key, val) {
$('#courseHist').append('<br />' + val.course+' - '+val.courseDate);
});
As expected, I'm getting a return of
正如所料,我得到了回报
1st Aid - 2016-04-25...
CPR - 2016-04-06...
ASL2 - 2016-03-25...
ASL1 - 2015-12-07...
Which is, technically, sorted by date descending. However, I need the return to sort by year descending, then month ascending, then date ascending. Like this:
从技术上讲,这是按日期降序排序的。但是,我需要返回按年份降序排序,然后按月升序,然后按日期升序排序。像这样:
ASL2 - 2016-03-25...
CPR - 2016-04-06...
1st Aid - 2016-04-25...
ASL1 - 2015-12-07...
I know I need to break my date return up into chunks and arrange from there but I just can't wrap my head around how to do that. Any help is greatly appreciated!
我知道我需要打破我的日期返回到大块并从那里安排,但我无法绕过如何做到这一点。任何帮助是极大的赞赏!
Alternately, I could do it on the PHP side if anyone has a solution for that.
或者,如果有人有解决方案,我可以在PHP方面做到这一点。
I have a fiddle HERE if you want to mess with it. - updated with working code from user blex
如果你想搞砸它我在这里有一个小提琴。 - 使用用户blex的工作代码进行更新
1 个解决方案
#1
2
Yo can split the data and sort it independently by year desc, month asc and day asc.
Yo可以分割数据并按年份desc,月份asc和day asc独立排序。
var data = [{ "course": "First Aid", "courseDate": "2016-04-25T00:00:00-06:00" }, { "course": "CPR", "courseDate": "2016-04-06T00:00:00-06:00" }, { "course": "ASL1", "courseDate": "2016-01-07T00:00:00-06:00" }, { "course": "ASL2", "courseDate": "2016-03-25T00:00:00-06:00" }, { "course": "ASL2X", "courseDate": "2015-03-25T00:00:00-06:00" }];
data.sort(function (a, b) {
var aa = a.courseDate.split(/\D/),
bb = b.courseDate.split(/\D/);
return bb[0] - aa[0] || aa[1] - bb[1] || aa[2] - bb[2];
});
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');
#1
2
Yo can split the data and sort it independently by year desc, month asc and day asc.
Yo可以分割数据并按年份desc,月份asc和day asc独立排序。
var data = [{ "course": "First Aid", "courseDate": "2016-04-25T00:00:00-06:00" }, { "course": "CPR", "courseDate": "2016-04-06T00:00:00-06:00" }, { "course": "ASL1", "courseDate": "2016-01-07T00:00:00-06:00" }, { "course": "ASL2", "courseDate": "2016-03-25T00:00:00-06:00" }, { "course": "ASL2X", "courseDate": "2015-03-25T00:00:00-06:00" }];
data.sort(function (a, b) {
var aa = a.courseDate.split(/\D/),
bb = b.courseDate.split(/\D/);
return bb[0] - aa[0] || aa[1] - bb[1] || aa[2] - bb[2];
});
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');