Jason Jason - 11 days ago 5
Android Question

How to re-sort JSON file based on value inside object Java Android

I am trying to parse and read a JSON file I get. I get an "appointmentInstance" in every object, and I would like to re-sort the whole file based on the appointmentInstance. Example:
If the JSONFile appointmentInstance has this sort of structure:

{
response: {
status: 200,
message: "",
details: "",
eventId: 0,
startRow: 0,
endRow: 9,
totalRows: 9,
data: [
{
id: 509955,
startTimeSlot: 5,
appointmentInstance: 310051
},
{
id: 509961,
startTimeSlot: 2,
appointmentInstance: 310057
},
{
id: 510070,
startTimeSlot: 3,
appointmentInstance: 310166
},
{
id: 510074,
startTimeSlot: 4,
appointmentInstance: 310170
},
{
id: 510522,
startTimeSlot: 6,
appointmentInstance: 310419
},
{
id: 510523,
startTimeSlot: 6,
appointmentInstance: 310420
},
{
id: 510524,
startTimeSlot: 7,
appointmentInstance: 310421
},
{
id: 510525,
startTimeSlot: 6,
appointmentInstance: 310060
},
{
id: 510535,
startTimeSlot: 7,
appointmentInstance: 310171
}
]
}
}


Then I would like it to be re-sorted to:

{
response: {
status: 200,
message: "",
details: "",
eventId: 0,
startRow: 0,
endRow: 9,
totalRows: 9,
data: [
{
id: 509955,
startTimeSlot: 5,
appointmentInstance: 310051
},
{
id: 509961,
startTimeSlot: 2,
appointmentInstance: 310057
},
{
id: 510525,
startTimeSlot: 6,
appointmentInstance: 310060
},
{
id: 510070,
startTimeSlot: 3,
appointmentInstance: 310166
},
{
id: 510074,
startTimeSlot: 4,
appointmentInstance: 310170
},
{
id: 510535,
startTimeSlot: 7,
appointmentInstance: 310171
}
{
id: 510522,
startTimeSlot: 6,
appointmentInstance: 310419
},
{
id: 510523,
startTimeSlot: 6,
appointmentInstance: 310420
},
{
id: 510524,
startTimeSlot: 7,
appointmentInstance: 310421
},
]
}
}


How can I do so? With my current code, I am able to "print out" the appointmentInstance, but it would of course not be printed in order.

My JSON:

{
response: {
status: 200,
message: "",
details: "",
eventId: 0,
startRow: 0,
endRow: 9,
totalRows: 9,
data: [
{
id: 509955,
startTimeSlot: 5,
appointmentInstance: 310051
},
{
id: 509961,
startTimeSlot: 2,
appointmentInstance: 310057
},
{
id: 510070,
startTimeSlot: 3,
appointmentInstance: 310166
},
{
id: 510074,
startTimeSlot: 4,
appointmentInstance: 310170
},
{
id: 510522,
startTimeSlot: 6,
appointmentInstance: 310419
},
{
id: 510523,
startTimeSlot: 6,
appointmentInstance: 310420
},
{
id: 510524,
startTimeSlot: 7,
appointmentInstance: 310421
},
{
id: 510525,
startTimeSlot: 6,
appointmentInstance: 310060
},
{
id: 510535,
startTimeSlot: 7,
appointmentInstance: 310171
}
]
}
}


My Java Android code:

try {
String thatarray = "{response: {status: 200,message: "",details: "",eventId: 0,startRow: 0,endRow: 9,totalRows: 9,data: [{id: 509955,startTimeSlot: 5,appointmentInstance: 310051},{id: 509961,startTimeSlot: 2,appointmentInstance: 310057},{id: 510070,startTimeSlot: 3,appointmentInstance: 310166},{id: 510074,startTimeSlot: 4,appointmentInstance: 310170},{id: 510522,startTimeSlot: 6,appointmentInstance: 310419},{id: 510523,startTimeSlot: 6,appointmentInstance: 310420},{id: 510524,startTimeSlot: 7,appointmentInstance: 310421},{id: 510525,startTimeSlot: 6,appointmentInstance: 310060},{id: 510535,startTimeSlot: 7,appointmentInstance: 310171}]}}";

JSONObject jsonObject = new JSONObject(thatarray);
jsonObject = jsonObject.getJSONObject("response");
JSONArray jsonArray = jsonObject.getJSONArray("data");

for(int i = jsonArray.length()-1; i>=0; i--){
jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getInt("appointmentInstance"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}

Answer

Solved by adding it to int[] appointments

Comments