I have a JSONArray with 5 different JSONObject and each of the 5 JSONObject has an identifier string value. The five values are "aa","bb","erer","cc","gg". My requirement is to get the JSONObject with identifier "erer" to first place and the following JSONObjects can be in any order. The json is:
{
"obj":[
{"identifier":"aa",},
{"identifier":"bb",},
{"identifier":"erer",},
{"identifier":"cc",},
{"identifier":"gg",}
]
}
The final result has to be "erer","aa","bb","cc","gg" and I need to do this in a single loop.
I'm able to do this in two loops.
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject j = jsonArray.getJSONObject(i);
if(j.getString("identifier").equals("erer")) {
sortedJson.put(joPayLoad);
}
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject j = jsonArray.getJSONObject(i);
if(!j.getString("identifier").equals("erer")) {
sortedJson.put(joPayLoad);
}
}
But the JsonArrray might also have 10,000 JSONObjects. And so, this 'two for-loops' will cause a performance lag. So, please help me to achieve my above said requirement in a single loop. Thanks in advance!
erer-Objects and one with the rest and then simply concat them after running through the for-loop once. If you use Java you could also useaddFirstoraddLastdepending on the object you are handling. Have a look at the documentation for these methodssortedJsonandjsonArray?