Objects in Java are sorted by attributes.

Source: Internet
Author: User
Tags collator

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.