Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort a JSONArray in JAVA [duplicate]

Tags:

java

json

sorting

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" }, ]; 
like image 805
kumarhimanshu449 Avatar asked Oct 23 '13 14:10

kumarhimanshu449


People also ask

How remove duplicates from JSONArray in Java?

You will need to convert the JSON to Java Objects and then perform the duplicate removal operation. Added code snippet for each of the steps. Hope this helps! You will need to convert the JSON to Java Objects and then perform the duplicate removal operation.

Can we sort JSON array?

JSON return type is an array of objects. Hence sort method cannot be used directly to sort the array. However, we can use a comparer function as the argument of the 'sort' method to get the sorting implemented.

How do you remove duplicates from an ArrayList in Java 8?

To remove the duplicates from the arraylist, we can use the java 8 stream api as well. Use steam's distinct() method which returns a stream consisting of the distinct elements comparing by object's equals() method. Collect all district elements as List using Collectors. toList() .


1 Answers

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.

like image 87
Vito Gentile Avatar answered Oct 07 '22 15:10

Vito Gentile