android - Sorting a JSONArray in java -
i'm working on api i'm getting jsonarray
in there reside numerous jsonobjects
containing around 10 keyvalue
pairs. 1 of them date: 10-3-2015
. want use jsonarray
in sorted manner according date value. i've tried various ways including treemap
no success yet.
a short piece of code or thorough idea work me. thanks.
edit:
you can sorting if use model hold element in jsonarray.let's have have model person , json array contains list of persons can traverse jsonarray , make list of persons follows:
list<person> persons=new arraylist<>(); if(jsonarray!=null){ for(jsonobject person:jsonarray){ person p=new person(); //set properties //............. //add persons persons.add(p); } }
now can sort list of persons follows:
collections.sort(persons, new comparator() { public int compare(object obj1, object obj2) { if (!(obj1 instanceof person) || !(obj2 instanceof person)) { return -1; } person p1 = (person)obj1; person p2 = (person)obj2; return p1.getage() -p2.getage(); } });
now can use sorted list "persons".
Comments
Post a Comment