3

I'm getting this string (from a webservice) into a JSONArray like this,

[
 {
  "lat": "-16.408545",
  "lon": "-71.539105",
  "type": "0",
  "distance": "0.54"
 },
 {
  "lat": "-16.4244317845",
  "lon": "-71.52562186",
  "type": "1",
  "distance": "1.87"
 },
 {
  "lat": "-16.4244317845",
  "lon": "-71.52562186",
  "type": "1",
  "distance": "0.22"
 }
]

I need to sort it by the distance key to show the nearest first and farthest last. I didn't try any code because I really don't have any ideas. I'm not using the GSON library, I'm using org.json.JSONArray.

2
  • use undercore.js library , you have various sorting solutions are with it. Commented Aug 5, 2015 at 10:04
  • please take a look at my question ..same issue Commented Oct 10, 2016 at 9:03

4 Answers 4

8

First parse your array in a list

JSONArray sortedJsonArray = new JSONArray();
List<JSONObject> jsonList = new ArrayList<JSONObject>();
for (int i = 0; i < jsonArray.length(); i++) {
    jsonList.add(jsonArray.getJSONObject(i));
}

then use collection.sort to sort the newly created list

Collections.sort( jsonList, new Comparator<JSONObject>() {

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

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

        return valA.compareTo(valB);
    }
});

Insert the sorted values in your array

for (int i = 0; i < jsonArray.length(); i++) {
    sortedJsonArray.put(jsonList.get(i));
}
Sign up to request clarification or add additional context in comments.

2 Comments

plz see this how to sort listview by name
to switch among the order --> valA.compareTo(valB) vs valB.compareTo(valA)
0

Parse your json object into a model say array-List and sort it using comparator.

ArrayList<ClassObject> dataList = new ArrayList<String>();
JSONArray array = new JSONArray(json);     
for(Object obj : jsonArray){
   dataList.add(//your data model);
}

Please refer this link for sorting of array-list http://java2novice.com/java-collections-and-util/arraylist/sort-comparator/

Comments

0

Try this. It should work

ArrayList<JSONObject> array = new ArrayList<JSONObject>();
JSONArray jsonArray = new JSONArray();
for (int i = 0; i < jsonArray.length(); i++) {
   try {
       array.add(jsonArray.getJSONObject(i));
   } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
}   

Collections.sort(array, new Comparator<JSONObject>() {

@Override
public int compare(JSONObject lhs, JSONObject rhs) {
    // TODO Auto-generated method stub

    try {
        return (lhs.getDouble("distance").compareTo(rhs.getDouble("distance")));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return 0;
    }
}
});

And after this you can convert sorted ArrayList array into JSONArray.

JSONArray jsonArray = new JSONArray(array);
String jsonArrayStr = jsonArray.toString();

Comments

0

In kotlin you can do as below

Parse your array in a list

val sortedJsonArray = JSONArray()
val jsonList = ArrayList<JSONObject>()
for (i in 0 until jsonArray.length()) {
    jsonList.add(jsonArray.getJSONObject(i))
}

Sort the newly created list

jsonList.sortWith { a, b ->
    var valA = String()
    var valB = String()
    try {
        valA = a.get("distance") as String
        valB = b.get("distance") as String
    } catch (e: JSONException) {
        e.printStackTrace()
    }
    valA.compareTo(valB)
}

Insert the sorted values in your json array

for (i in 0 until jsonArray.length()) {
    sortedJsonArray.put(jsonList[i])
}

Thanks to @crysis for his answer.

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.