The class in the generic collection is Jsonobject, not a custom class, and if it is a custom class, take the value of the field you want to compare directly.
1Arraylist<jsonobject> TList =NewArraylist<jsonobject>();2 3 for(inti=0;i<10000;i++)4 {5Jsonobject object=NewJsonobject ();6Random Rand =NewRandom ();7 intRandnum = Rand.nextint (2000000) +5;8String value=string.valueof (randnum);9 Try {TenObject.put ("Value", Value); OneObject.put ("name", "Liaohang"); A}Catch(jsonexception e) { - //TODO auto-generated Catch block - e.printstacktrace (); the } - Tlist.add (object); - - + - } + A at -Collections.sort (TList,NewComparator<jsonobject>() { - //can change ' Name ' with ' ID ' if you want to sort by ID - Private Static FinalString key_name = "value"; - - @Override in Public intCompare (Jsonobject A, jsonobject b) { - intValA =0; to intValb =0; + - Try { theValA =integer.parseint (String) a.get (key_name)); *Valb =integer.parseint (String) b.get (key_name)); $ } Panax Notoginseng Catch(jsonexception e) { - //Do something the } + returnValA > Valb? 1:-1; A the //return Vala.compareto (VALB); + //If you want to change the sort order, simply use the following: - //Return-vala.compareto (VALB); $ } $ }); - - the //print a sorted result -String printvalue=NULL;Wuyi for(intI=0;i<tlist.size (); i++) the { -Jsonobject obect=Tlist.get (i); Wu Try { -Printvalue=obect.getstring ("Value"); About}Catch(jsonexception e) { $ //TODO auto-generated Catch block - e.printstacktrace (); - } - A System.out.println (printvalue); + the}
Ordering members in a Java paradigm collection