Reputation: 4278
I want to sort an object with objects in, and order by date. I get my objects from an API who delivers their API like this:
{
"2529354": {
"id": "2529354",
"name": "Burton Albion-Barnsley",
"tournament_stageFK": "850172",
"startdate": "2017-10-31T19:45:00+00:00",
"status_type": "notstarted",
"status_descFK": "1",
"n": "1",
"ut": "2017-06-21T10:32:10+00:00",
"tournamentFK": "11549",
"tournament_templateFK": "48",
"sportFK": "1",
"tournament_stage_name": "Championship",
"tournament_name": "2017/2018",
"tournament_template_name": "England 2",
"sport_name": "Soccer"
},
"2529355": {
"id": "2529355",
"name": "Cardiff City-Ipswich Town",
"tournament_stageFK": "850172",
"startdate": "2017-10-31T20:45:00+00:00",
"status_type": "notstarted",
"status_descFK": "1",
"n": "1",
"ut": "2017-06-21T10:32:10+00:00",
"tournamentFK": "11549",
"tournament_templateFK": "48",
"sportFK": "1",
"tournament_stage_name": "Championship",
"tournament_name": "2017/2018",
"tournament_template_name": "England 2",
"sport_name": "Soccer"
},
"2529364": {
"id": "2529364",
"name": "Reading-Nottingham Forest",
"tournament_stageFK": "850172",
"startdate": "2017-10-31T19:00:00+00:00",
"status_type": "notstarted",
"status_descFK": "1",
"n": "1",
"ut": "2017-06-21T10:32:11+00:00",
"tournamentFK": "11549",
"tournament_templateFK": "48",
"sportFK": "1",
"tournament_stage_name": "Championship",
"tournament_name": "2017/2018",
"tournament_template_name": "England 2",
"sport_name": "Soccer"
}
};
It could be hard to notice, but the startdate is different in those 3 objects. The API delivers like 17 objects, or games in this context, and they are not in order. I have tried using the sort-function, but as it is for arrays it doesn't really work.
Is there a possibility to sort it with Object.keys(object) or something similar?
Upvotes: 0
Views: 256
Reputation: 86
You can figure out the order of the keys and put them in an array like so:
var sortedKeys = Object.keys(data).sort((a, b) => {
return new Date(data[b].startdate) - new Date(data[a].startdate);
});
Then you can access your data in the sorted order like this
sortedKeys.each(function (key) {
// do something with data[key]
});
Upvotes: 1
Reputation: 26143
You can't sort this object as the properties are the keys, and therefore not sortable. You can push the individual objects into an array and sort that instead though...
var data = {
"2529354": {
"id": "2529354",
"name": "Burton Albion-Barnsley",
"startdate": "2017-10-31T19:45:00+00:00"
},
"2529355": {
"id": "2529355",
"name": "Cardiff City-Ipswich Town",
"startdate": "2017-10-31T20:45:00+00:00"
},
"2529364": {
"id": "2529364",
"name": "Reading-Nottingham Forest",
"startdate": "2017-10-31T19:00:00+00:00"
}
};
var dataArray = [];
for (var i in data) {
dataArray.push(data[i]);
}
dataArray.sort(function(x, y) {
var xDate = new Date(x.startdate);
var yDate = new Date(y.startdate);
if (xDate < yDate) return -1
else if (xDate > yDate) return 1
else return 0;
});
console.log(dataArray);
Note: I trimmed the data for display purposes. Just use the whole thing when you use it.
Upvotes: 1