Sorting a JSONArray by one of its values

I have a JSONArray which saves “Name” and “Best”. Best is always an int. I would like to be able to sort the array descending. I can not provide any code as there is none. Thanks in advance.

String[] names = { "A", "B", "C" };
JSONArray values;

void setup() {

  values = new JSONArray();

  for (int i = 0; i < names.length; i++) {

    JSONObject exp = new JSONObject();

    exp.setInt("id", (int)(Math.random()*10));
    exp.setString("name", names[i]);

    values.setJSONObject(i, exp);
  }
  
  // id like to sort this descending by the id
}

Edit: i was tired of this problem and solved it the worst way possible, creating an array for name and one for time, then using

Collections.sort(time, Collections.reverseOrder()); 

and then creating a jsonobject from that again.

Take a Look at this page :
https://stackoverflow.com/questions/19543862/how-can-i-sort-a-jsonarray-in-java

1 Like

Thanks, but as I said, I have been unable to utilize this in processing.

I‘m sorry, But i can‘t See that you said you can‘t utilize this anywhere :sweat_smile:

Btw, how can you have a JSONArray, if you don‘t have a Code?

Oh, I thought I wrote that i wasnt able to utilize the examples found on google in processing.
I have no code regarding my question. Added some slightly edited code from processing.org/reference/JSONArray.html as I dont plan posting a 1k+ lines project for this question

Uhm… ok since i still don‘t quite understand why you weren‘t able to utilize the method described in the link, i‘ll just modify the code you posted, so that it sorts it. (I assume „id“ should actually be „best“)

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.json.JSONException;

String[] names = { "A", "B", "C" };
JSONArray values;

void setup() {

  values = new JSONArray();

  for (int i = 0; i < names.length; i++) {

    JSONObject exp = new JSONObject();

    exp.setInt("id", (int)(Math.random()*10));
    exp.setString("name", names[i]);

    values.setJSONObject(i, exp);
  }
  JSONArray sorted = sort(values, "id", true); //change „id“ to „name“ to sort by name and 0 to 1 to reverse sort order
  // id like to sort this descending by the id
}

void draw() {

}

JSONArray sort(JSONArray jsonArr, String sortBy, boolean sortOrder) {
    JSONArray sortedJsonArray = new JSONArray();

    List<JSONObject> jsonValues = new ArrayList<JSONObject>();
    for (int i = 0; i < jsonArr.size(); i++) {
        jsonValues.add(jsonArr.getJSONObject(i));
    }
            final String KEY_NAME = sortBy;
        final Boolean SORT_ORDER = sortOrder;
    Collections.sort( jsonValues, new Comparator<JSONObject>() {

        @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) {
                //exception
            }
            if (SORT_ORDER) {
              return valA.compareTo(valB);
            } else {
              return -valA.compareTo(valB);
            }
        }
    });
        return sortedJsonArray;
}
1 Like