JSONObject数组排序工具类

时间:2022-09-03 14:28:54

 

依赖jar

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.56</version>
        </dependency>

 

JsonUtils.java

package javax.utils;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * JSONObject数组排序工具类
 * 
 * @author Logan
 * @createDate 2019-04-10
 * @version 1.0.0
 *
 */
public class JsonUtils {

    /**
     * 按指定key排序JSONObject对象数组
     * 
     * @param array JSONObject对象数组
     * @param key 指定排序key
     */
    public static void sort(JSONObject[] array, String key) {
        Arrays.sort(array, new Comparator<JSONObject>() {

            @Override
            public int compare(JSONObject a, JSONObject b) {
                return JsonUtils.compare(a, b, key);
            }
        });
    }

    /**
     * 按指定key排序JSONObject对象集合
     * 
     * @param list JSONObject对象集合
     * @param key 指定排序key
     */
    public static void sort(List<JSONObject> list, String key) {
        Collections.sort(list, new Comparator<JSONObject>() {

            @Override
            public int compare(JSONObject a, JSONObject b) {
                return JsonUtils.compare(a, b, key);
            }
        });
    }

    /**
     * 按指定key降序排序JSONObject对象数组
     * 
     * @param array JSONObject对象数组
     * @param key 指定排序key
     */
    public static void sortDesc(JSONObject[] array, String key) {
        Arrays.sort(array, new Comparator<JSONObject>() {

            @Override
            public int compare(JSONObject a, JSONObject b) {
                return -1 * JsonUtils.compare(a, b, key);
            }
        });
    }

    /**
     * 按指定key降序排序JSONObject对象集合
     * 
     * @param list JSONObject对象集合
     * @param key 指定排序key
     */
    public static void sortDesc(List<JSONObject> list, String key) {
        Collections.sort(list, new Comparator<JSONObject>() {

            @Override
            public int compare(JSONObject a, JSONObject b) {
                return -1 * JsonUtils.compare(a, b, key);
            }
        });
    }

    /**
     * 按指定key比较两个JSONObject对象大小
     * 
     * @param a 第一个JSONObject对象
     * @param b 第二个JSONObject对象
     * @param key 指定进行比较的key
     * @return
     *         <ul>
     *             <li>如果a==b,返回0</li>
     *             <li>如果a>b,返回1</li>
     *             <li>如果a<b,返回-1</li>
     *         </ul>
     */
    public static int compare(JSONObject a, JSONObject b, String key) {
        Object va = a.get(key);
        Object vb = b.get(key);

        if (null == va && null == vb) {
            return 0;
        }

        if (null == va) {
            return -1;
        }

        if (null == vb) {
            return 1;
        }

        if (va.equals(vb)) {
            return 0;
        }

        if (va instanceof Number && vb instanceof Number) {

            /* 取double值相减,兼容整数 */
            if (a.getDoubleValue(key) - b.getDoubleValue(key) > 0) {
                return 1;
            }

            else {
                return -1;
            }
        }

        return a.getString(key).compareToIgnoreCase(b.getString(key));
    }

}

 

 

 

 

以下是测试程序

 

JsonTest.java

package com.java.test;

import java.io.File;
import java.net.URL;
import java.util.List;

import javax.utils.JsonUtils;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author Logan
 * @createDate 2019-04-10
 * @version 1.0.0
 *
 */
public class JsonTest {

    private static final String encoding = "UTF-8";

    @Test
    public void sort() {
        try {
            URL resource = JsonTest.class.getResource("/JsonText.txt");
            String text = FileUtils.readFileToString(new File(resource.getFile()), encoding);

            String key = "birthday";

            List<JSONObject> list = JSON.parseArray(text, JSONObject.class);
            System.out.println(JSON.toJSONString(list, true));
            System.out.println();

            JsonUtils.sort(list, key);
            System.out.println(JSON.toJSONString(list, true));
            System.out.println();

            JsonUtils.sortDesc(list, key);
            System.out.println(JSON.toJSONString(list, true));
            System.out.println();

            System.out.println("-------------------------------------------");
            JSONArray jsonArray = JSON.parseArray(text);
            JSONObject[] array = jsonArray.toArray(new JSONObject[jsonArray.size()]);
            System.out.println(JSON.toJSONString(array, true));
            System.out.println();

            JsonUtils.sort(array, key);
            System.out.println(JSON.toJSONString(array, true));
            System.out.println();

            JsonUtils.sortDesc(array, key);
            System.out.println(JSON.toJSONString(array, true));
            System.out.println();
            System.out.println("-------------------------------------------");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

JsonText.txt

[{
    "birthday":"2000-12-21",
    "number":"10003",
    "name":"Hello",
    "weight":120.5,
    "age":16
},{
    "birthday":"1998-12-21",
    "number":"10002",
    "name":"Hi",
    "weight":150.5,
    "age":18
},{
    "birthday":"2006-12-21",
    "number":"10005",
    "name":"Good",
    "weight":130.5,
    "age":10
}]

 

 

 

 

.