排序(3) : JSONArray排序

参考 : 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);
        }});
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要对一个JSONArray进行排序,你可以按照以下步骤进行操作: 1. 首先,将JSONArray转换为List对象,以便更方便地进行排序。你可以使用JSONArray的toList()方法来实现这一点。 2. 接下来,使用Collections类的sort()方法对List进行排序。根据你的需求,你可以选择使用默认的自然排序或自定义排序。 - 自然排序:如果JSONArray中的元素是基本数据类型(如整数、字符串等),则可以直接使用Collections.sort()方法进行排序。 - 自定义排序:如果JSONArray中的元素是自定义对象,你需要实现Comparator接口,并重写compare()方法来定义对象的比较规则。 3. 最后,如果需要,将排序后的List转换回JSONArray。你可以使用JSONArray的构造函数来创建一个新的JSONArray对象,并提供排序后的List作为参数。 下面是一个示例代码,展示了如何对JSONArray进行排序: ```java import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class JsonArraySortingExample { public static void main(String[] args) { JSONArray jsonArray = new JSONArray("[5, 2, 8, 1, 10]"); // Step 1: Convert JSONArray to List List<Integer> list = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { try { list.add(jsonArray.getInt(i)); } catch (JSONException e) { e.printStackTrace(); } } // Step 2: Sort the list Collections.sort(list); // Step 3: Convert List back to JSONArray (if needed) JSONArray sortedJsonArray = new JSONArray(list); System.out.println(sortedJsonArray); } } ``` 在上面的示例中,我们首先将JSONArray转换为List,然后使用Collections.sort()方法对List进行排序。最后,将排序后的List转换回JSONArray,并打印出结果。 你可以根据自己的需求进行适当的修改和调整。希望能对你有所帮助!如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值