Sorting ArrayList of Objects

here is a sort using comparator

see How can I sort JSON by ID?



// Import 
import java.util.Comparator;

// Main List  
JSONArray valuesArray1= new JSONArray();

// ----------------------------------------------------------------

void setup() {
  size(600, 600); 

  // build up test data 
  String[] species = { "Panthera pardus", "Equus zebra", "Capra hircus" };
  String[] names = {  "Leopard", "Zebra", "Goat" };

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

    JSONObject animal = new JSONObject();

    animal.setInt("id", i);
    animal.setString("species", species[i]);
    animal.setString("name", names[i]);

    valuesArray1.setJSONObject(i, animal);
  }
}

void draw() {
  // print, sort and print again 
  printlnJSONArray(valuesArray1);
  valuesArray1 = sort1();
  printlnJSONArray(valuesArray1);

  // halt
  noLoop();
} //

// ----------------------------------------------------------------

void printlnJSONArray( JSONArray values ) {
  
  println ("-------------------\n"
    +"printlnJSONArray says: "); 

  for (int i = 0; i < values.size(); i++) {

    JSONObject animal = values.getJSONObject(i); 

    int id = animal.getInt("id");
    String species = animal.getString("species");
    String name = animal.getString("name");

    println(id + ", " + species + ", " + name);
  }
}

// ----------------------------------------------------------------

JSONArray sort1() {

  // load objects into a List (ArrayList, etc)
  ArrayList<JSONObject> myobjs = new ArrayList<JSONObject>(); 

  for (int i = 0; i < valuesArray1.size(); i++) {
    JSONObject currentArticle = valuesArray1.getJSONObject(i); 
    myobjs.add(currentArticle);
  }

  // sort the new list
  myobjs.sort(new JSONComparator());

  // create an array to put the sorted list in
  JSONArray jsonSorted = new JSONArray();

  // append the objects in order
  for (JSONObject obj : myobjs) {
    jsonSorted.append(obj);
  }

  // now your sorted list of objects are loaded in a JSONArray
  return jsonSorted;
  //
}//func 

// ======================================================================================================================

// Tool takes care of comparator(s): needed for sorting the List 

// we can either sort like in a lexicon (ABCD... -> 0 111 22 3 4444 56) or numeric (0 3 22 56 111....)  

// this will fail on very long IDs like 9923898494443; maybe using Long helps 

// sorting for the List 
class JSONComparator implements Comparator<JSONObject> {

  @Override
    public int compare (JSONObject a, JSONObject b) {

    // this will fail on very long IDs like 9923898494443; maybe using Long helps

    // The following notation is short for these if clauses:
    // if a<b return -1;
    //   if a==b return 0;
    //   else return 1;
    //return aIndex < bIndex ? -1 : 
    //  aIndex.equals(bIndex) ? 0 :
    //  1;

    String aIndex = a.getString("species");
    String bIndex = b.getString("species");

    return 
      aIndex.compareToIgnoreCase( bIndex ); // we sort like in a lexicon (ABCD...) 
    //
  }
}//class
//
2 Likes