-1

i have a list with json objects. i want sort that list of json objects based one single key. that key A1,A1.1,A1.1.1,A2 contained those values. based on that key values i want to sort that list in java.

For ex:

[{
    "age_from": 0.0,
    "age_to": 72.0,
    "answer": 0.0,
    "answer_display_type": 0.0,
    "gender": 0.0,
    "gid": "A1",
    "group": "A.Defects at Birth",
    "id": 269.0,
},{
    "age_from": 0.0,
    "age_to": 72.0,
    "answer": 0.0,
    "answer_display_type": 0.0,
    "gender": 0.0,
    "gid": "A1.1",
    "group": "A.Defects at Birth",
    "id": 269.0,
},{
    "age_from": 0.0,
    "age_to": 72.0,
    "answer": 0.0,
    "answer_display_type": 0.0,
    "gender": 0.0,
    "gid": "A1.1.1",
    "group": "A.Defects at Birth",
    "id": 269.0,
},{
    "age_from": 0.0,
    "age_to": 72.0,
    "answer": 0.0,
    "answer_display_type": 0.0,
    "gender": 0.0,
    "gid": "A2",
    "group": "A.Defects at Birth",
    "id": 269.0,
}] 
RyanNerd
  • 2,695
  • 1
  • 20
  • 27
  • 1
    What have you tried so far ? You can use Jackson to extract the records to a map, with the key set to your key field, and then sort that. – jr593 Oct 18 '19 at 09:09
  • Collections.sort(questionsResult, (obj1, obj2) -> { return obj1.get("gid").toString().replaceAll(" ", "").compareTo(obj2.get("gid").toString().replaceAll(" ", "")); }); i have tried like this up to now. it works only 50 % . i.e if A1,A1.1,A1.1.1,A10 ,A1.2 coming as a result – bhargav samineni Oct 18 '19 at 09:17

2 Answers2

0
public static void main(String[] args) {
        String jsonArrStr = "YOUR JSON ARRAY STRING"; // Replace this string with your json array
        JSONArray jsonArr = new JSONArray(jsonArrStr);
        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>() {
            private static final String KEY_NAME = "gid";

            @Override
            public int compare(JSONObject a, JSONObject b) {
                String valA = "";
                String valB = "";
                try {
                    valA = (String) a.get(KEY_NAME);
                    valB = (String) b.get(KEY_NAME);
                }
                catch (JSONException e) {
                }
                return valA.compareTo(valB);
            }
        });

        System.out.println(jsonValues);
    }
}
Pawan Maurya
  • 357
  • 1
  • 9
0

You can map each json record to an object and then using comparator/comparable it can easily be sorted on a specific key.

You can use object mapper to map json to Object

Example : You have to sort on basis of age

    class Sample{
    int age;
    String name;
    }

    class SortByAge implements Comparator<Sample> 
    { 
        // Used for sorting in ascending order of age 
        public int compare(Sample a, Sample b) 
        { 
            return a.getAge() - b.getAge(); 
        } 
    } 

// it will return a sorted list based on age

    Collections.sort(ar, new SortByAge());