# Sorting a JSONArray by one of its values

**URL:** https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911
**Category:** Coding Questions
**Created:** [October 26, 2018, 8:35pm UTC](https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911 "2018-10-26T20:35:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![delsey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/delsey/32/11380_2.png) [@delsey](https://discourse.processing.org/u/delsey)
#### Post date: [October 26, 2018, 8:35pm UTC](https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911/1 "2018-10-26T20:35:05Z")

</div>

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.

```auto
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

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

and then creating a jsonobject from that again.

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 26, 2018, 9:11pm UTC](https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911/2 "2018-10-26T21:11:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![delsey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/delsey/32/11380_2.png) [@delsey](https://discourse.processing.org/u/delsey)
#### Post date: [October 27, 2018, 9:10pm UTC](https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911/3 "2018-10-27T21:10:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 27, 2018, 9:15pm UTC](https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911/4 "2018-10-27T21:15:36Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![delsey](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/delsey/32/11380_2.png) [@delsey](https://discourse.processing.org/u/delsey)
#### Post date: [October 28, 2018, 9:00am UTC](https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911/5 "2018-10-28T09:00:55Z")

</div>

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](http://processing.org/reference/JSONArray.html) as I dont plan posting a 1k+ lines project for this question

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 28, 2018, 12:00pm UTC](https://discourse.processing.org/t/sorting-a-jsonarray-by-one-of-its-values/4911/6 "2018-10-28T12:00:21Z")

</div>

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“)

```auto
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;
}

```
