I am working on a complex JSON file.
I load the entire file (list of items A and each item has different JSON-Arrays and JSON-Objects in it).
I don’t copy it into an Array of a class but just work with the loaded entire JSON file. Is that ok/recommended?
When displaying the file, I like to sort the items A by their ID (number like 2022234). How is that possible?
I suggest you look into comparators. If you can make a comparator that takes in your objects or arrays and reduces it to a value then it would be fairly simple to sort numerically using Collections.sort().
Not entirely sure how your data is coming in and haven’t used these in a while so I don’t think this is right but should give you a basic idea of what I’m thinking. Might have to add some imports in processing for collections or comparator.
Collections.sort(/* your JSONArray */, new Comparator<JSONArray>() {
public int compare(JSONArray json1, JSONArray json2) {
return json1.id - json2.id;
}
});
Comparator sorts units. If you have a JSONArray of JSONObjects, you need to sort those objects in pairwise comparisons. This requires a List data structure that contains them. So:
copy the objects from the JSONArray to a List,
sort the List object, passing .sort() your Comparator for JSONObjects so it will sort in that way
copy the objects from the now-sorted List into a new JSONArray.
List<Foo> myfoos;
Comparator<Foo> fooComp;
myfoos.sort(fooComp); // now your list of Foos is sorted
sort() on a list of Foos takes a Comparator that works on pairs of individual Foos, Foo a, Foo b – the sort method uses the Comparator throughout the list until all the Foos are sorted. So your comparator should be for JSONObject a, JSONObject b, like this (untested):
class JSONObjectIDComparator implements Comparator<JSONObject> {
@Override
public int compare (JSONObject a, JSONObject b) {
return a.getInt("id") - b.getInt("id");
}
}
So. Given
a JSONArray of JSONObjects, json
a class JObjComparator implements Comparator // NOT JSONArray
and then given that you have loaded your objects into a List (ArrayList, etc):
JSONArray --> List myobjs;
then (pseudocode)
// sort your list
myobjs.sort(new JSONObjectIDComparator());
// create an array to put the sorted list in
jsonSorted = new JSONArray();
// append the objects in order
for(JSONObject obj : myobjs){
jsonA.append(obj)
}
// now your sorted list of objects are loaded in a JSONArray
float() is just a helper function in Processing – which is standardized on float, so it doesn’t have it long().
Use Long.valueOf() for your strings, and Long.compare() to compare two longs. (untested)
class JSONComparator implements Comparator<JSONObject> {
@Override
public int compare (JSONObject a, JSONObject b) {
return Long.compare(Long.valueOf(a.getString("id")), Long.valueOf(b.getString("id")));
}
}
So how do you want these sorted? As Strings? Or are there specific rules to the prefixes, like that they are always 3 letters long, and do you want to sort the prefixes alphabetically and the suffixes numerically?