Issue
I am trying to sort my json data by date but its not working. here is what I am trying. please correct me where I am making mistake
Sample Code
var temp = [{
"id": 17608,
"title": "abc",
"start": "2016-03-23 06:13:00.0",
"backgroundColor": "#000000",
"borderColor": "#000000",
"textColor": "#fff"
}, {
"id": 17608,
"title": "def",
"start": "2016-04-13 06:13:00.0",
"backgroundColor": "#000000",
"borderColor": "#000000",
"textColor": "#fff"
}, {
"id": 17608,
"title": "ghi",
"start": "2016-04-08 06:13:00.0",
"backgroundColor": "#000000",
"borderColor": "#000000",
"textColor": "#fff"
}];
console.log(temp);
temp.sort(function(a, b) {
if (new Date(a.start) == new Date(b.start)) {
return a.row == b.row ? 0 : +a.row > +b.row ? 1 : -1;
}
return new Date(a.start) > (b.start) ? 1 : -1;
});
console.log(temp);
Solution
You can use the date string for sorting, while it is an ISO 6801 date.
var temp = [{ "id": 17608, "title": "abc", "start": "2016-03-23 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }, { "id": 17608, "title": "def", "start": "2016-04-13 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }, { "id": 17608, "title": "ghi", "start": "2016-04-08 06:13:00.0", "backgroundColor": "#000000", "borderColor": "#000000", "textColor": "#fff" }];
temp.sort(function (a, b) {
return a.start.localeCompare(b.start);
});
document.write("<pre>" + JSON.stringify(temp, 0, 4) + "</pre>");
Answered By - Nina Scholz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.