JSONArray中按欄位排序
阿新 • • 發佈:2021-12-17
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONException; import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo { public staticvoid main(String[] args) { String jsonArrStr = "[{"ID":"A01","Name":"張三","Score":88},{"ID":"A02","Name":"李四","Score":99},{"ID":"A01","Name":"王五","Score":77}]"; System.out.println("排序前:" + jsonArrStr); String jsonArraySort = jsonArraySort(jsonArrStr); System.out.println("排序後:" + jsonArraySort); } public static String jsonArraySort(String jsonArrStr) { // json字串轉為JSONArray JSONArray jsonArr = JSON.parseArray(jsonArrStr); //存放排序結果json陣列 JSONArray sortedJsonArray = new JSONArray(); // 用於排序的list List<JSONObject> list = newArrayList<JSONObject>(); //遍歷待排序的json陣列,並將資料放入list for (int i = 0; i < jsonArr.size(); i++) { list.add(jsonArr.getJSONObject(i)); } Collections.sort(list, new Comparator<JSONObject>() { //排序欄位 private static final String KEY_NAME1 = "ID"; private static final String KEY_NAME2 = "Score"; @Override public int compare(JSONObject a, JSONObject b) { String valA1 = new String(); String valA2 = new String(); String valB1 = new String(); String valB2 = new String(); try { valA1 = a.getString(KEY_NAME1); valA2 = b.getString(KEY_NAME1); valB1 = a.getString(KEY_NAME2); valB2 = b.getString(KEY_NAME2); } catch (JSONException e) { System.out.println(e); } // 設定排序規則 int i = valA1.compareTo(valA2); if (i == 0) { int j = valB1.compareTo(valB2); return j; } return i; } }); //將排序後結果放入結果jsonArray for (int i = 0; i < jsonArr.size(); i++) { sortedJsonArray.add(list.get(i)); } return sortedJsonArray.toString(); } }
結果:
排序前:[{"ID":"A01","Name":"張三","Score":88},{"ID":"A02","Name":"李四","Score":99},{"ID":"A01","Name":"王五","Score":77}]
排序後:[{"Score":77,"ID":"A01","Name":"王五"},{"Score":88,"ID":"A01","Name":"張三"},{"Score":99,"ID":"A02","Name":"李四"}]
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.Comparator; public class Test2 { public static void main(String[] args) { String str = "[{"score":77,"id":"A04"},{"score":88,"id":"A01"},{"score":66,"id":"A01"}]"; System.out.println("排序前: " + str); JSONArray array = JSON.parseArray(str); for (Object arr : array) { System.out.println(arr); } // 方式一 // array.sort(Comparator.comparing(obj -> ((JSONObject)obj).getInteger("score"))); // array.sort(Comparator.comparing(e-> ((JSONObject)e).getString("id"))); // 方式二:單欄位排序 // array.sort((a, b) -> ((JSONObject) a).getString("id").compareTo(((JSONObject) b).getString("id"))); // 方式二:多欄位排序 array.sort((a, b) -> { int i = ((JSONObject) a).getString("id").compareTo(((JSONObject) b).getString("id")); if (i == 0) { int j = ((JSONObject) a).getInteger("score").compareTo(((JSONObject) b).getInteger("score")); return j; } return i; }); System.out.println("排序後: " + array); for (Object arr : array) { System.out.println(arr); } } }
結果:
資料來自:http://t.zoukankan.com/wakey-p-12306429.html