I'm trying to create a list of "events" for an agenda. To do this, I recieve a JSON response which contains several month objects, which in turn contain an array of event-objects.
When I receive my response, all months are ordered, as shown below:
{
"Agenda/Future": {
"November 2011/0": [
{
"id": "5710",
"eventid": "",
"name": "test7",
"startdate": "nov 9",
"datecompute": "2011-11-09T12:00:00",
"group_month": "November 2011",
"flg_Data": "Database"
},
{
"id": "5707",
"eventid": "",
"name": "test4",
"startdate": "nov 17",
"datecompute": "2011-11-17T12:00:00",
"group_month": "November 2011",
"flg_Data": "Database"
},
{
"id": "5704",
"eventid": "",
"name": "test",
"startdate": "nov 30",
"datecompute": "2011-11-30T12:00:00",
"group_month": "November 2011",
"flg_Data": "Database"
}
],
"December 2011/1": [
{
"id": "5705",
"eventid": "",
"name": "test2",
"startdate": "dec 28",
"datecompute": "2011-12-28T12:00:00",
"group_month": "December 2011",
"flg_Data": "Database"
}
],
"November 2013/2": [
{
"id": "5706",
"eventid": "",
"name": "test3",
"startdate": "nov 1",
"datecompute": "2013-11-01T12:00:00",
"group_month": "November 2013",
"flg_Data": "Database"
},
{
"id": "5708",
"eventid": "",
"name": "test5",
"startdate": "nov 15",
"datecompute": "2013-11-15T12:00:00",
"group_month": "November 2013",
"flg_Data": "Database"
},
{
"id": "5709",
"eventid": "",
"name": "test6",
"startdate": "nov 15",
"datecompute": "2013-11-15T12:00:00",
"group_month": "November 2013",
"flg_Data": "Database"
}
]
}
}
First there is November 2011, then December 2011, and finally November 2013.
However after parsing this response, the order has changed to November 2011, November 2013, December 2011. Like so:
Obviously, this can cause quite some confusion.
My theory is that due to the use of JSONArray eventNames = agenda.names();
, which gets the names of the month objects, the order changes to one based on the actual month, not the year. This has been confirmed after adding another event in March 2022, and this event ends up first.
I checked through my code, and nowhere can I find any sort of function that would change the ordering, making the names();
the most probably culprit.
My question is, how can I make sure the objects in my initial JSONArray
get ordered by the month + year, rather then just the month. Removing the names()
method is not an option, considering the fact that the objectnames for the months are dynamic (based on month+year). I also would rather not change to a different parsing system (GSON, Jackson), since this is the only one that has been succesfull at parsing my reponse (see previous asked questions).
Eventually managed to fix it by looping over an array containing the full names, and then checking whether that name contained any of the parts from the ordered list (the one containing the years + id). If the string contains that part, add it to a new array on the position of the year+id it contained. Code can be found below
String[] test = new String[eventNames.length()];
String[] test2;
for (int x = 0; x < eventNames.length(); x++){
//System.out.println(eventNames.get(x).toString());
String[] xx = eventNames.get(x).toString().split(" ");
test[x] = xx[1];
}
Arrays.sort(test);
String[] test3 = new String[eventNames.length()];
for(int q = 0; q < eventNames.length(); q++){
String str = eventNames.get(q).toString();
for(int p = 0; p < test.length; p++){
if(str.contains(test[p])){
test3[p] = str;
}
}
}
System.out.println(test.toString());
for (int x = 0; x < eventNames.length(); x++){
System.out.println("maand :"+x+" "+test3[x]);
}