排序(3) : JSONArray排序

该代码示例展示了如何使用阿里巴巴的FastJSON库结合Java的Collections.sort方法,根据JSONObject中的Double类型键值对JSONArray进行排序。主要方法包括sortByKeyDouble和sortByKeyDoubleDesc,支持单个键和两个键的排序,处理了键值可能为空的情况。

参考 : JSONArray排序_擦肩而过的博客-优快云博客


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);
        }});
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值