参考 : JSONArray排序_擦肩而过的博客-CSDN博客
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @Author: liyue
* @Date: 2023/05/25/16:45
* @Description:
*/
public class JSONArraySortUtil {
public static void main(String[] args) {
JSONArray ja = new JSONArray();
addTest(ja);
JSONArray ja2 = sortByKeyDouble(ja, "id", "id2");
System.out.println();
System.out.print("原 id: ");
for (int i = 0; i < ja.size(); i++) {
JSONObject j = ja.getJSONObject(i);
Integer id = j.getInteger("id");
System.out.print("[" + id + "]");
}
System.out.println();
System.out.print("新 id: ");
for (int i = 0; i < ja2.size(); i++) {
JSONObject j = ja2.getJSONObject(i);
Integer id = j.getInteger("id");
System.out.print("[" + id + "]");
}
System.out.println();
System.out.println("两个字段排序");
System.out.print("原 : ");
for (int i = 0; i < ja.size(); i++) {
JSONObject j = ja.getJSONObject(i);
Integer id = j.getInteger("id");
Integer id2 = j.getInteger("id2");
System.out.print("[" + id + "-" + id2 + "]");
}
System.out.println();
System.out.print("新 : ");
for (int i = 0; i < ja2.size(); i++) {
JSONObject j = ja2.getJSONObject(i);
Integer id = j.getInteger("id");
Integer id2 = j.getInteger("id2");
System.out.print("[" + id + "-" + id2 + "]");
}
System.out.println();
}
private static JSONArray sortByKeyDouble(JSONArray jsonArray, String key) {
List<JSONObject> list = JSONArray.parseArray(jsonArray.toJSONString(), JSONObject.class);
Collections.sort(list, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
Double a = o1.getDouble(key);
Double b = o2.getDouble(key);
if (a > b) {
return 1;
} else if (a == b) {
return 0;
} else {
return -1;
}
}
});
return JSONArray.parseArray(list.toString());
}
private static JSONArray sortByKeyDoubleDesc(JSONArray jsonArray, String key) {
List<JSONObject> list = JSONArray.parseArray(jsonArray.toJSONString(), JSONObject.class);
Collections.sort(list, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
Double a = o1.getDouble(key);
Double b = o2.getDouble(key);
if (a > b) {
return 1;
} else if (a == b) {
return 0;
} else {
return -1;
}
}
});
return JSONArray.parseArray(list.toString());
}
private static JSONArray sortByKeyDouble(JSONArray jsonArray, String key1, String key2) {
List<JSONObject> list = JSONArray.parseArray(jsonArray.toJSONString(), JSONObject.class);
Collections.sort(list, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
Double a = o1.getDouble(key1);
Double b = o2.getDouble(key1);
Double a2 = o1.getDouble(key2);
Double b2 = o2.getDouble(key2);
if (a2 == null || b2 == null) {
return getCompare(a, b);
} else {
if (a > b) {
return 1;
} else if (a.equals(b)) {
return getCompare(a2, b2);
} else {
return -1;
}
}
}
});
return JSONArray.parseArray(list.toString());
}
private static JSONArray sortByKeyDoubleDesc(JSONArray jsonArray, String key1, String key2) {
List<JSONObject> list = JSONArray.parseArray(jsonArray.toJSONString(), JSONObject.class);
Collections.sort(list, new Comparator<JSONObject>() {
@Override
public int compare(JSONObject o1, JSONObject o2) {
Double a = o1.getDouble(key1);
Double b = o2.getDouble(key1);
Double a2 = o1.getDouble(key2);
Double b2 = o2.getDouble(key2);
if (a2 == null || b2 == null) {
return getCompareDesc(a, b);
} else {
if (a > b) {
return -1;
} else if (a.equals(b)) {
return getCompareDesc(a2, b2);
} else {
return 1;
}
}
}
});
return JSONArray.parseArray(list.toString());
}
public static Integer getCompare(Double a, Double b) {
if (a > b) {
return 1;
} else if (a.equals(b)) {
return 0;
} else {
return -1;
}
}
public static Integer getCompareDesc(Double a, Double b) {
if (a > b) {
return -1;
} else if (a.equals(b)) {
return 0;
} else {
return 1;
}
}
public static void addTest(JSONArray ja) {
ja.add(new JSONObject() {{
put("id", 4);
put("id2", 2);
}});
ja.add(new JSONObject() {{
put("id", 4);
put("id2", 3);
}});
ja.add(new JSONObject() {{
put("id", 4);
put("id2", 5);
}});
ja.add(new JSONObject() {{
put("id", 4);
put("id2", 2);
}});
ja.add(new JSONObject() {{
put("id", 1);
put("id2", 2);
}});
ja.add(new JSONObject() {{
put("id", 2);
put("id2", 4);
}});
ja.add(new JSONObject() {{
put("id", 5);
put("id2", 4);
}});
ja.add(new JSONObject() {{
put("id", 6);
put("id2", 4);
}});
}
}