In the list found in the database, different fields often need to be re-sorted. Generally, the sorting field is used to re-query in the database. If the database query is not found, it is directly sorted in the list obtained for the first time, which will undoubtedly improve the system performance. As long as you store the results obtained for the first time in the session, you can re-sort the list. Collections. sort (list) can be used to sort lists. However, this method does not work if the list contains an object. If the following object is available: [java] // UserInfo. java package test; import java. text. simpleDateFormat; public class UserInfo implements java. io. serializable {private java. lang. integer userId; private java. lang. string username; private java. util. date birthDate; private java. lang. integer age; private SimpleDateFormat formater = new SimpleDateFormat ("yyyy-MM-dd"); public UserInfo () {} public UserInfo (java. lang. integer us ErId, java. lang. string username, java. util. date birthDate, java. lang. integer age) {this. userId = userId; this. username = username; this. birthDate = birthDate; this. age = age;} public void setUserId (java. lang. integer value) {this. userId = value;} public java. lang. integer getUserId () {return this. userId;} public void setUsername (java. lang. string value) {this. username = value;} public java. la Ng. string getUsername () {return this. username;} public void setBirthDate (java. util. date value) {this. birthDate = value;} public java. util. date getBirthDate () {return this. birthDate;} public void setBirthDatestr (String value) throws Exception {setBirthDate (formater. parse (value);} public java. lang. string getBirthDatestr () {return formater. format (getBirthDate ();} public void setAge (java. la Ng. integer value) {this. age = value;} public java. lang. integer getAge () {return this. age;} public String toString () {return new StringBuffer (). append (getUserId ()). append (";" + getUsername ()). append (";" + getBirthDatestr ()). append (";" + getAge ()). toString () ;}} is a simple data object. To sort userids, you must use the code similar to the following: [java] Collections. sort (list, new Comparator () {public int compare (Object a, Obje Ct B) {int one = (Order) ). getUserId (); int two = (Order) B ). getUserId (); return one-two ;}}); to sort the fields in the UserInfo list, do you want to write the code shown above for each field? That is certainly not the result we need. Write programs must be more refined and not more redundant. Can I write a general method? The answer is yes, but the following three problems must be solved first: 1. you can use generics. 2. can use common comparison methods, such as compareTo; 3. is there any generic method like generic or generic? 1st problems can be solved, and 2nd problems are not very difficult, because all Java types are inherited from objects and there is a ToString method, at present, all types can be converted to String, and then compared with compareTo. 3rd problems. We have not yet found the generic method we need. However, we can use the getMethod and invoke dynamic retrieval methods. The complete code is as follows: [java] // SortList. java package test; import java. util. collections; import java. util. comparator; import java. util. list; import java. lang. reflect. method; import java. lang. reflect. invocationTargetException;/*** @ author jardot * @ version 1.0 * General sorting */public class SortList <E> {public void Sort (List <E> list, final String method, final String sort) {Collections. sort (list, new Comparator () {publ Ic int compare (Object a, Object B) {int ret = 0; try {Method m1 = (E) ). getClass (). getMethod (method, null); Method m2 = (E) B ). getClass (). getMethod (method, null); if (sort! = Null & "desc ". equals (sort) // reverse ret = m2.invoke (E) B), null ). toString (). compareTo (m1.invoke (E) a), null ). toString (); else // forward ret = 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 );} Return ret ;}}) ;}} success! The above Code does not use specific objects and types, and is already universal. We use a generic E. If we want to sort the userId of UserInfo, the method name can be passed in as a string with parameters, for example, "getUserId ". You can use the following code to Test: [java] // Test. java package test; import java. util. arrayList; import java. util. list; import java. text. simpleDateFormat; public class Test {public static void main (String [] args) throws Exception {List <UserInfo> list = new ArrayList <UserInfo> (); simpleDateFormat formater = new SimpleDateFormat ("yyyy-MM-dd"); list. add (new UserInfo (3, "B", formater. parse ("1980-12-01"), 11); list. add (new Use RInfo (1, "c", formater. parse ("1980-10-01"), 30); list. add (new UserInfo (2, "a", formater. parse ("1973-10-01"), 11); System. out. println ("------- original sequence -------------------"); for (UserInfo user: list) {System. out. println (user. toString ();} // call the sorting generic class SortList <UserInfo> sortList = new SortList <UserInfo> (); // sort sortList by userId. sort (list, "getUserId", "desc"); System. out. println ("-------- in descending order of userId ------------- ----- "); For (UserInfo user: list) {System. out. println (user. toString ();} // sort sortList by username. sort (list, "getUsername", null); System. out. println ("--------- sort by username -----------------"); for (UserInfo user: list) {System. out. println (user. toString ();} // sort sortList by birthDate. sort (list, "getBirthDatestr", null); System. out. println ("--------- sort by birthDate -----------------"); for (UserInfo user: List) {System. out. println (user. toString () ;}} run the following results: ------- original sequence ----------------- 3; B; 1980-12-01; 111; c; 1980-10-01; 302; a; 1973-10-01; 11 -------- in descending order of userId ---------------- 3; B; 1980-12-01; 112; a; 1973-10-01; 111; c; 1980-10-01; 30 --------- sort by username; a; 1973-10-01; 113; B; 1980-12-01; 111; c; 1980-10-01; 30 --------- sort by birthDate --------------- 2; a; 1973-10-01; 111; C; 1980-10-01; 303; B; 1980-12-01; 11 Note: the order of dates is first sorted by format conversion. Otherwise, no correct result is returned.