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 static void 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 = new ArrayList<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":"李四"}]
Lambda 表达式
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); } } }
结果: