Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java – sort JSONArray based on two attribute values

I have a JSONArray as below,

JSONArray dataArray = new JSONArray();
dataArray = [
    {
        "name": "name1",
        "row": 1,
        "value": 20
    },
    {
        "name": "name2",
        "row": 1,
        "value": 10
    },
    {
        "name": "name3",
        "row": 2,
        "value": 10
    },
    {
        "name": "name4",
        "row": 3,
        "value": 30
    },
    {
        "name": "name5",
        "row": 3,
        "value": 10
    }
]

I need to compare the row attribute, if same, need to compare value attribute and sort the object in the array.

Tried with Java comparator, but couldn’t make it work. Can somebody please help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

            
   for(int i = 0; i < dataArray.size(); i++) {
       elementList.add((JSONObject) dataArray.get(i));
   }
   Long row1 = null;
   for (JSONObject obj : elementList) {
       if(row1 == null) {
           row1 = (Long) ((JSONObject) obj.get("row"));
       }
       else {
            Long row2 = (Long) ((JSONObject) obj.get("row"));
            if(row2 == row1) {
                //call the comparator, but how to pass two objects?
                //Collections.sort(elementList, new abc.xComparator());
            }
            row1 = row2;
       }
   }

>Solution :

It would be easy to extend this answer to match your scenario

But instead of

 return valA.compareTo(valB);

you should do

     int comp = valA.compareTo(valB);
        if (comp == 0){
            String valC = (String) a.get(KEY_NAME2);
            String valD = (String) b.get(KEY_NAME2);
            return valC.compareTo(valD);
        } else {
            return comp;
        }

So it should be the following.

    JSONArray sortedJsonArray = new JSONArray();
    List<JSONObject> jsonValues = new ArrayList<JSONObject>();
    for (int i = 0; i < dataArray.length(); i++) {   // <---- dataArray is the input that you have
        jsonValues.add(dataArray.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_NAME1 = "row";
        private static final String KEY_NAME2 = "value";

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

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

            int comp = valA.compareTo(valB);
            if (comp == 0){
                String valC = (String) a.get(KEY_NAME2);
                String valD = (String) b.get(KEY_NAME2);
                return valC.compareTo(valD);
            } else {
                return comp;
            }
        }
    });

Edit: changed into KEY_NAME1 = "row"; to match the new question requirement

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading