I am trying to print the top 5 entries based on total.amount from an api response array.
The response looks something like this, but a lot longer:
[
{
"total": {
"amount": "100000"
},
"name": {
"first": "Test"
},
},
{
"total": {
"amount": "500"
},
"name": {
"first": "Test2"
},
}
]
At the moment I am able to print all the amount values in descending order:
public static void orderResultsDesc(int total) throws JSONException {
JsonPath jsonPathValidator = response.jsonPath();
List<String> totAm = jsonPathValidator.getList("total.amount");
try {
totAm.sort(Comparator.reverseOrder());
System.out.println("List:\n" + totAm);
} catch (Exception e) {
e.printStackTrace();
}
}
But I need to adjust this to print the top 5 entries with a whole response (not just the total.amount values).
Have tried doing things like:
List<Integer> ids = jsonPathValidator.getList("total.amount");
for(Integer i:ids)
{
ids.sort(Comparator.reverseOrder());
sortedOrder.add(ids); // this doesn't work
System.out.println(i);
}
It's new territory for me.. Any help is appreciated!!