I have an unsorted array of players that came through a JSON response:
"participants": [{
"participant": {
"rank": 3,
"name": "I Tied for third",
}
}, {
"participant": {
"rank": 1,
"name": "I got first",
}
}, {
"participant": {
"rank": 3,
"name": "Also tied for third",
}
}, {
"participant": {
"rank": 2,
"name": "I got second",
}
}]
I would like to sort this somehow so that I can eventually print out both the player's name, and their rank... but in order.
I thought about possibly putting the results of the array into a TreeMap, but then I realised that TreeMaps require unique array keys and that wouldn't work:
Map<Integer, String> players = new TreeMap<>();
for (int i = 0; i < participants.length(); i++) {
JSONObject object = participants.getJSONObject(i);
JSONObject participant = object.getJSONObject("participant");
players.put(participant.getInt("rank"), participant.getString("name"));
}
So is there another way to get this done?