It is often used in projects. For example, the region list is sorted by the first letter of the Chinese alphabet, but sometimes the obtained region list is in the form of an object, with the Region id and region name attributes. There are two main technical points involved: 1. sorting objects by attributes; 2. Sorting Chinese characters by the first letter of pinyin. The demo is provided below:
Import java. text. collator; import java. util. arrayList; import java. util. collections; import java. util. comparator; import org. apache. commons. beanutils. beanComparator; import org. apache. commons. collections. comparatorUtils; import org. apache. commons. collections. comparators. comparableComparator; import org. apache. commons. collections. comparators. comparatorChain; public class CompareTipA {private int id; private String name; private String age; @ SuppressWarnings ("unchecked") public static void main (String [] args) {ArrayList <Object> list = new ArrayList <Object> (); list. add (new CompareTipA (1, "3", "5"); list. add (new CompareTipA (2, "1", "6"); list. add (new CompareTipA (5, "5", "2"); list. add (new CompareTipA (3, "4", "4"); list. add (new CompareTipA (8, "4", "4"); list. add (new CompareTipA (4, "2", "1"); Comparator mycmp = ComparableComparator. getInstance (); mycmp = ComparatorUtils. nullLowComparator (mycmp); // allow null mycmp = ComparatorUtils. reversedComparator (mycmp); // reverse ArrayList <Object> sortFields = new ArrayList <Object> (); Comparator <Object> comparator = Collator. getInstance (java. util. locale. CHINA); sortFields. add (new BeanComparator ("name", comparator); // name (main) sortFields. add (new BeanComparator ("id", mycmp); // id Reverse Order (secondary) ComparatorChain multiSort = new ComparatorChain (sortFields); Collections. sort (list, multiSort); for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I); // output/* [id = 5, name = 2, age = 5] [id = 2, name = 6, age = 1] [id = 8, name = 4, age = 4] [id = 3, name = 4, age = 4] [id = 1, name = 5, age = 3] [id = 4, name = 1, age = 2] */} public CompareTipA (int id, String age, String name) {this. id = id; this. name = name; this. age = age;} public int getId () {return this. id;} public void setId (int id) {this. id = id;} public String getName () {return this. name;} public void setName (String name) {this. name = name;} // public int compareTo (Object o) {// return this. comparator. compare (this, o); //} public String toString () {return "[id =" + this. id + ", name =" + this. name + ", age =" + this. age + "]";} public String getAge () {return age;} public void setAge (String age) {this. age = age ;}}
Note: The three packages to be imported: commons-beanutils-1.8.3.jar commons-collections-3.2.1.jar commons-logging-1.1.3.jarcommons-collections package cannot use the official website version 4.0, because from the source code can see commons-beanutils package called is the commons-collections3.2 version of the api, 4.0 is not compatible; the commons-logging package is also required by the commons-beanutils package.