本文介绍了如何在 JAVA 中对 JSONArray 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按对象的字段对对象的 JSONArray 进行排序?

输入:

[{ "ID": "135", "Name": "Fargo Chan" },{ "ID": "432", "Name": "Aaron Luke" },{ "ID": "252", "Name": "Dilip Singh" }];

所需的输出(按名称"字段排序):

[{ "ID": "432", "Name": "Aaron Luke" },{ "ID": "252", "Name": "Dilip Singh" }{ "ID": "135", "Name": "Fargo Chan" },];
解决方案

试试这个:

//我假设我们需要从以下字符串创建一个 JSONArray 对象String jsonArrStr = "[ { "ID": "135", "Name": "Fargo Chan" },{ "ID": "432", "Name": "Aaron Luke" },{ "ID": "252", "Name": "Dilip Singh" }]";JSONArray jsonArr = 新的 JSONArray(jsonArrStr);JSONArray sortedJsonArray = new JSONArray();列表jsonValues = new ArrayList();for (int i = 0; i < jsonArr.length(); i++) {jsonValues.add(jsonArr.getJSONObject(i));}Collections.sort( jsonValues, new Comparator() {//如果要按ID排序,可以将名称"更改为ID"私有静态最终字符串KEY_NAME =名称";@覆盖公共 int 比较(JSONObject a,JSONObject b){String valA = new String();String valB = new String();尝试 {valA = (String) a.get(KEY_NAME);valB = (String) b.get(KEY_NAME);}捕获(JSONException e){//做一点事}返回 valA.compareTo(valB);//如果要更改排序顺序,只需使用以下命令://返回-valA.compareTo(valB);}});for (int i = 0; i < jsonArr.length(); i++) {sortedJsonArray.put(jsonValues.get(i));}

排序后的 JSONArray 现在存储在 sortedJsonArray 对象中.

How to sort a JSONArray of objects by object's field?

Input:

[
    { "ID": "135", "Name": "Fargo Chan" },
    { "ID": "432", "Name": "Aaron Luke" },
    { "ID": "252", "Name": "Dilip Singh" }
];

Desired output (sorted by "Name" field):

[
    { "ID": "432", "Name": "Aaron Luke" },
    { "ID": "252", "Name": "Dilip Singh" }
    { "ID": "135", "Name": "Fargo Chan" },
];
解决方案

Try this:

    //I assume that we need to create a JSONArray object from the following string
    String jsonArrStr = "[ { "ID": "135", "Name": "Fargo Chan" },{ "ID": "432", "Name": "Aaron Luke" },{ "ID": "252", "Name": "Dilip Singh" }]";

    JSONArray jsonArr = new JSONArray(jsonArrStr);
    JSONArray sortedJsonArray = new JSONArray();

    List<JSONObject> jsonValues = new ArrayList<JSONObject>();
    for (int i = 0; i < jsonArr.length(); i++) {
        jsonValues.add(jsonArr.getJSONObject(i));
    }
    Collections.sort( jsonValues, new Comparator<JSONObject>() {
        //You can change "Name" with "ID" if you want to sort by ID
        private static final String KEY_NAME = "Name";

        @Override
        public int compare(JSONObject a, JSONObject b) {
            String valA = new String();
            String valB = new String();

            try {
                valA = (String) a.get(KEY_NAME);
                valB = (String) b.get(KEY_NAME);
            }
            catch (JSONException e) {
                //do something
            }

            return valA.compareTo(valB);
            //if you want to change the sort order, simply use the following:
            //return -valA.compareTo(valB);
        }
    });

    for (int i = 0; i < jsonArr.length(); i++) {
        sortedJsonArray.put(jsonValues.get(i));
    }

The sorted JSONArray is now stored in the sortedJsonArray object.

这篇关于如何在 JAVA 中对 JSONArray 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 01:25