Premise:
The elements in the list are custom objects, how can they be sorted according to the elements of the object?
For example, List<student>students is a list, each element is a Student object, Student object has a member variable name,age, and so on,
So I want to sort by age, how do I do that?
1, using the comparator interface
The student class structure is as follows: ( omit Getter,setter method )
Public classStudent {/*** * Name*/ PrivateString name; Private intAge ; PrivateString address; /*** * Test score*/ Private intscore;//Omit Getter,setter Method@Override PublicString toString () {return"Student [name=" + name + ", age=" + Age + ", score=" +score+ "]"; } }
Test method:
@Test Public voidTest_listcomparator () {List<student>students=NewArraylist<student>(); Student Stu=NULL; Stu=NewStudent (); Stu.setname ("Whuang"); Stu.setage (12); Stu.setscore (80); Students.add (Stu); Stu=NewStudent (); Stu.setname ("Rong"); Stu.setage (11); Stu.setscore (90); Students.add (Stu); Stu=NewStudent (); Stu.setname ("Zhu"); Stu.setage (15); Stu.setscore (100); Students.add (Stu); Collections.sort (Students,NewSystemhwutil. Listcomparator (true, "Age")); SYSTEM.OUT.PRINTLN (students); }
Operation Result:
[Student [Name=rong, age=11, score=90], Student [Name=whuang, Age=12, score=80], Student [Name=zhu, Age=15, score=100]]
Core class:
Public Static classListcomparatorImplementscomparator{/*** * Whether to convert to int and then compare*/ Private BooleanIsconvertinteger; /*** * Which column to sort*/ PrivateString Comparedproperty; PublicListcomparator (Booleanisconvertinteger,string Comparedproperty) { Super(); This. Isconvertinteger =Isconvertinteger; This. comparedproperty=Comparedproperty; } Public intCompare (Object O1, Object O2) {if(NULL!=o1&&NULL!=O2) { Try{Object obj1=Reflecthwutils.getobjectvalue (O1, Comparedproperty); Object Obj2=Reflecthwutils.getobjectvalue (O2, Comparedproperty); if(Isconvertinteger) {intNUM1; intnum2; if(obj1instanceofInteger) {NUM1=(Integer) obj1; Num2=(Integer) obj2; }Else{NUM1=Integer.parseint (obj1.tostring ()); Num2=Integer.parseint (obj2.tostring ()); } if(num1>num2) { return1; }Else if(num1<num2) { return-1; }Else{ return0; } }Else{ returnobj1.tostring (). CompareTo (Obj2.tostring ()); } } Catch(SecurityException e) {e.printstacktrace (); } Catch(nosuchfieldexception e) {e.printstacktrace (); } Catch(IllegalArgumentException e) {e.printstacktrace (); } Catch(illegalaccessexception e) {e.printstacktrace (); } } return0/*equals*/; } }
2, you can specify whether ascending or descending
Instance:
@Test Public voidTest_listcomparator () {List<student>students=NewArraylist<student>(); Student Stu=NULL; Stu=NewStudent (); Stu.setname ("Whuang"); Stu.setage (12); Stu.setscore (80); Students.add (Stu); Stu=NewStudent (); Stu.setname ("Rong"); Stu.setage (11); Stu.setscore (90); Students.add (Stu); Stu=NewStudent (); Stu.setname ("Zhu"); Stu.setage (15); Stu.setscore (100); Students.add (Stu); Sortlist<Student> sortlist =NewSortlist<student>(); Sortlist.sort (Students,"Getage", "ASC"); SYSTEM.OUT.PRINTLN (students); }
Note: The second parameter of Sortlist.sort is the method name , not the member variable name.
Core code
PackageCom.common.util;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List; Public classSortlist<e> { Public voidSort (list<e> List,FinalString method,FinalString Sort) {Collections.sort (list,NewComparator () { Public intCompare (Object A, object B) {intRET = 0; Try{Method M1= ((E) a). GetClass (). GetMethod (Method,NULL); Method m2= ((E) b). GetClass (). GetMethod (Method,NULL); if(Sort! =NULL&& "desc". Equals (sort))//Reverseret = M2.invoke (((E) b),NULL). ToString (). CompareTo (M1.invoke (((E) a),NULL). toString ()); Else //Positive Orderret = M1.invoke (((E) a),NULL). ToString (). CompareTo (M2.invoke (((E) b),NULL). toString ()); } Catch(nosuchmethodexception ne) {System.out.println (NE); } Catch(Illegalaccessexception IE) {System.out.println (IE); } Catch(InvocationTargetException it) {System.out.println (IT); } returnret; } }); }}
Reference:
http://hw1287789687.iteye.com/blog/2223537
Sort list by Java