PublicList sort (List list,FinalString Sortname,FinalString direction) {Collections.sort (list,NewComparator () { Public intCompare (Object A, object B) {Try { //get to field based on property nameField field =A.getclass (). Getdeclaredfield (Sortname); //field is set to public access property.Field.setaccessible (true); //gets the type of the propertyClass Typeclass =Field.gettype (); //int Sort if(Typeclass.equals (Integer.class) ) {Integer one=(Integer) Field.get (a); Integer=(Integer) field.get (b); if("desc". Equals (direction)) { returnDouble-One ; } Else { returnOne-both ; } } Else{//string SortString one =(String) Field.get (a); String of=(String) field.get (b); if("desc". Equals (direction)) { returnTwo.compareto (one); } Else { returnOne.compareto (both); } } } Catch(Exception e) {stringwriter SW=NewStringWriter (); E.printstacktrace (NewPrintWriter (SW,true)); } return1; } }); returnlist; }}
List in Java sorted by a property