0

I am able to sort JSONArray of JSONObject but I stuck when I need to sort array like follows:

[{
    "key1": "value1",
    "key2": "value2",
    "key3": {
        "key4": "value4",
        "key5": "value5"
    }
}, {
    "key1": "value1",
    "key2": "value2",
    "key3": {
        "key4": "value4",
        "key5": "value5"
    }
}]

I want to sort this array on "Key4".

Here is my code while sorting on key1

public static JSONArray sortJsonArray(JSONArray array, final String key) throws JSONException 
{
        List<JSONObject> jsons = new ArrayList<JSONObject>();
        for (int i = 0; i < array.length(); i++) {
            jsons.add(array.getJSONObject(i));
        }
        Collections.sort(jsons, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject lhs, JSONObject rhs) {
                String lid = null;
                String rid = null;
                try {
                    lid = lhs.getString(key);
                    rid = rhs.getString(key);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                // Here you could parse string id to integer and then compare.
                return lid.compareTo(rid);
            }
        });
        return new JSONArray(jsons);
    }
3
  • As far as I know, JSONArray is not a sortable data-structure, you'd need to put the JSONObjects into a List<JSONObject> then sort Commented Mar 14, 2016 at 20:05
  • Right, I put that but I can sort only on Key1 or key2; How I can sort that on key4? Commented Mar 14, 2016 at 20:07
  • @cricket_007: I added my code in question. Commented Mar 14, 2016 at 20:11

2 Answers 2

1

Just compare lhs.getJSONObject("key3").getString("key4") to rhs.getJSONObject("key3").getString("key4").

Sign up to request clarification or add additional context in comments.

Comments

0

Just pasting my code Here(As per @cricket_007 Helps), May this help some others:

public static JSONArray sortJsonArrayOpenDate(JSONArray array) throws JSONException {
        List<JSONObject> jsons = new ArrayList<JSONObject>();
        for (int i = 0; i < array.length(); i++) {
            jsons.add(array.getJSONObject(i));
        }
        Collections.sort(jsons, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject lhs, JSONObject rhs) {
                String lid = null;
                String rid = null;
                try {
                    lid = lhs.getJSONObject("key3").getString("key4");
                    rid = rhs.getJSONObject("key3").getString("key4");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return lid.compareTo(rid);
            }
        });
        return new JSONArray(jsons);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.