Java Essentials Collection--sorting lists using the Collections.sort method

Source: Internet
Author: User

To sort the list, you can let the entity object implement the comparable interface , and override the CompareTo method to sort by an attribute, but this is a single way to sort by a fixed attribute, without change. By following this method, you can dynamically specify that the list is sorted by one of the attributes, the example is simple, see the code to understand.

  

Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List; Public classtestcollection {/**     * @paramargs*/     Public Static voidMain (string[] args) {User u1=NewUser (); U1.setname ("Jessica"); U1.setage (20); User U2=NewUser (); U2.setname ("Abobo"); U2.setage (30); List<User> userlist=NewArraylist<user>();        Userlist.add (U1);        Userlist.add (U2); Mycomparator Sort=NewMycomparator (); MyComparator2 Sort2=NewMyComparator2 (); MyComparator3 Sort3=NewMyComparator3 (); MyComparator4 Sort4=NewMyComparator4 ();        Collections.sort (Userlist,sort);  for(User u:userlist) {System.out.println (u.tostring ()+ "; I am" + (Userlist.indexof (u) +1) + "and I ' m ordered by Name Desc");        } collections.sort (USERLIST,SORT2);  for(User u:userlist) {System.out.println (u.tostring ()+ "; I am" + (Userlist.indexof (u) +1) + "and I ' m ordered by Name ASC");        } collections.sort (USERLIST,SORT3);  for(User u:userlist) {System.out.println (u.tostring ()+ "; I am" + (Userlist.indexof (u) +1) + "and I ' m ordered by" DESC ");        } collections.sort (USERLIST,SORT4);  for(User u:userlist) {System.out.println (u.tostring ()+ "; I am" + (Userlist.indexof (u) +1) + "and I ' m ordered by age ASC"); }    }     Public Static classMycomparatorImplementsComparator<user>{@Override Public intCompare (user O1, user O2) {return-o1.getname (). CompareTo (O2.getname ()); }            }     Public Static classMyComparator2ImplementsComparator<user>{@Override Public intCompare (user O1, user O2) {returno1.getname (). CompareTo (O2.getname ()); }            }     Public Static classMyComparator3ImplementsComparator<user>{@Override Public intCompare (user O1, user O2) {return-o1.getage (). CompareTo (O2.getage ()); }            }     Public Static classMyComparator4ImplementsComparator<user>{@Override Public intCompare (user O1, user O2) {returno1.getage (). CompareTo (O2.getage ()); }            }}

Results:

Hello! My Name is Jessica and I ' M 20;i am 1 and i 'm ordered by Name Deschello! My Name is Abobo and I ' M 30;i am 2 and I 'm ordered by Name Deschello! My Name is Abobo and I ' M 30;i am 1 and i 'm ordered by Name Aschello! My Name is Jessica and I ' M 20;i am 2 and I 'm ordered by Name Aschello! My Name is Abobo and I ' M 30;i am 1 and i 'm orderedby age Deschello! My Name is Jessica and I ' M 20;i am 2 and I 'm orderedby age Deschello! My Name is Jessica and I ' M 20;i am 1 and i 'm orderedby age Aschello! My Name is Abobo and I ' M 30;i am 2 and I ' M ordered by age ASC

This way, you can choose to sort the list of the way, lay a good foundation, in order to gradually stabilize the Great wall.

Java Essentials Collection--sorting lists using the Collections.sort method

Related Article

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.