码迷,mamicode.com
首页 > 编程语言 > 详细

jsonArray中按字段排序

时间:2020-02-14 10:39:12      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:array   compare   规则   ++   void   exception   private   span   for   

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":"李四"}]

jsonArray中按字段排序

标签:array   compare   规则   ++   void   exception   private   span   for   

原文地址:https://www.cnblogs.com/wakey/p/12306429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!