Application Scenarios:
The List<object> collection is often encountered in development and is sorted according to a property of the objects in the collection-------- the following solution to this
Public Static FinalString desc = "desc"; Public Static FinalString ASC = "ASC"; /*** Purpose: Sort a List Collection array * * Description: * Currently available for list<java.lang.class>, list<pojo>, List<map > Sort these three types of collections * *@paramCollection Object for list sort operation *@paramProperty Specifies the sort field of the element in the collection, if the collection element is not an object type can be passed to a value of NULL *@paramsort is used to specify whether ascending or descending collectionsutil.desc--descending collectionsutil.asc--ascending * @date 2018.04.27 PM*/ Public Static<T>voidSortonlist (list<t> List,FinalObject Property,FinalString Sort) {Collections.sort (list,NewComparator<t>() {@Override Public intCompare (t O1, T O2) {Integer A1=NULL; Integer A2=NULL; if(O1instanceofInteger) {//for list<integer>A1 =(Integer) O1; A2=(Integer) O2; } Else if(O1instanceofString) {//for list<string>A1 =integer.valueof (o1.tostring ()); A2=integer.valueof (o2.tostring ()); } Else if(O1instanceofMAP) {//for list<map<string,string> typesMap Temp1 =(MAP) O1; Map Temp2=(MAP) O2; Object Object=Temp1.get (property); if(ObjectinstanceofInteger) {A1=(Integer) object; A2=(Integer) Temp2.get (property); } Else if(ObjectinstanceofString) {//Sort by value in map string type needs to be convertedA1 =Integer.parseint (object.tostring ()); A2=Integer.parseint (Temp2.get (property). ToString ()); } } Else{//for object TypesClass C1 =O1.getclass (); Class C2=O2.getclass (); Try{Field DeclaredField1=C1.getdeclaredfield (property.tostring ()); Field declaredField2=C2.getdeclaredfield (property.tostring ()); Declaredfield1.setaccessible (true); Declaredfield2.setaccessible (true); A1=Integer.parseint (Declaredfield1.get (O1). toString ()); A2=Integer.parseint (Declaredfield2.get (O2). toString ()); } Catch(Exception e) {e.printstacktrace (); } } if(Sort.equals (COLLECTIONSUTIL.ASC))//Ascending returnA1.compareto (A2); Else //Descending returnA2.compareto (A1); } }); }
The following code principle:
Collections.sort (list,new comparator<t>() {
@Override public int compare (t O1, T O2) {
}
}
Compares a O1 to a numeric attribute of a O2 object based on the elements in the collection:
o1-o2 >= 0 --- ascending
O1-o2 < 0 --- descending
The Sort method sorts a property of an object in the list collection, for example:
PackageHello1;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List;ImportHello. Student; Public classMain { Public Static voidMain (string[] args) {Student S1=NewStudent (); S1.setid (6); Student S2=NewStudent (); S2.setid (66); Student S3=NewStudent (); S3.setid (1); Student S4=NewStudent (); S4.setid (55); List<Student> list =NewArraylist<>(); List.add (S1); List.add (S2); List.add (S3); List.add (S4); System.out.println ("Unsorted Result:" +list); //Sort by Student Object ID propertyCollections.sort (list,NewComparator<student>() {@Override Public intCompare (Student O1, Student O2) {returnO1.getid ()-O2.getid (); } }); System.out.println ("Post-order results:" +list); }}
Run result ====================== "
Welcome to the passing buddies.
Ordering elements in the list collection