I am attempting to sort this JSON object by price lowest to highest using this example How to sort JSON object in java?. But when i try to display my list it appear's unsorted. I am not sure what I am doing wrong can someone point me in the right direction. I read several post already and they all say to use collection to sort a JSON object, i attempted implementing it below, but it does not work any suggestion is appreciated.
json :-
[ {
"symbol" : "SAN",
"companyName" : "Banco Santander, S.A.",
"marketCap" : 62296657920,
"sector" : "Financial Services",
"industry" : "Banks—Diversified",
"beta" : 1.74298500000000000653699316899292171001434326171875,
"price" : 3.5800000000000000710542735760100185871124267578125,
"lastAnnualDividend" : 0.24899999999999999911182158029987476766109466552734375,
"volume" : 4284228,
"exchange" : "New York Stock Exchange",
"exchangeShortName" : "NYSE",
"country" : "ES",
"isEtf" : false,
"isActivelyTrading" : true
}, {
"symbol" : "ABEV",
"companyName" : "Ambev S.A.",
"marketCap" : 48729493504,
"sector" : "Consumer Defensive",
"industry" : "Beverages—Brewers",
"beta" : 0.93046200000000001129052407122799195349216461181640625,
"price" : 3.0099999999999997868371792719699442386627197265625,
"lastAnnualDividend" : 0.0970000000000000028865798640254070051014423370361328125,
"volume" : 24947634,
"exchange" : "New York Stock Exchange",
"exchangeShortName" : "NYSE",
"country" : "BR",
"isEtf" : false,
"isActivelyTrading" : true
}, {
"symbol" : "LYG",
"companyName" : "Lloyds Banking Group plc",
"marketCap" : 41814462464,
"sector" : "Financial Services",
"industry" : "Banks—Regional",
"beta" : 1.483138999999999985135445967898704111576080322265625,
"price" : 2.310000000000000053290705182007513940334320068359375,
"lastAnnualDividend" : 0.07022699999999999775912584709658403880894184112548828125,
"volume" : 5825636,
"exchange" : "New York Stock Exchange",
"exchangeShortName" : "NYSE",
"country" : "GB",
"isEtf" : false,
"isActivelyTrading" : true
},
code :-
public void downloadChangePercentage(String api) {
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
api,
null,
new Response.Listener<JSONArray>() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onResponse(JSONArray response) {
// ArrayList<JSONObject> array = new ArrayList<JSONObject>();
ArrayList<JSONObject> jsonStockList = new ArrayList<>();
for (int i = 0; i < response.length(); i++) {
// LOOP over JSON objects and added it to mode/ Stocklist arraylist..
try {
stockInfo = response.getJSONObject(i);
stockDetail = new StockDetails(stockInfo.getString("changesPercentage"), stockInfo.getString("symbol"), stockInfo.getString("price"), stockInfo.getString("name"));
stockListArrayList.add(stockDetail);
jsonStockList.add(response.getJSONObject(i));
searchStockAdapter.notifyDataSetChanged();
ArrayList<JSONObject> sortedStockList = sortJSONObject(jsonStockList);
for (int j = 0; j < sortedStockList.size(); j++) {
logText("sortedData", String.valueOf(sortedStockList.get(j)));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("error", error.toString());
}
});
// Add JsonArrayRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
}
ArrayList<JSONObject> sortJSONObject(ArrayList<JSONObject> jsonStockList){
Collections.sort(jsonStockList, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
try {
return Double.compare(o1.getDouble("price"), o2.getDouble("price"));
} catch (JSONException e) {
e.printStackTrace();
return 0;
}
}
});
return jsonStockList;
}