Comparable and comparator of a property comparison sort for an object in a Java collection

Source: Internet
Author: User
Tags comparable

There are two ways to sort an attribute of an object in a collection.

A. One is to sort the object class to implement the comparable interface CompareTo method, then put the object into the list, and then call Collections.sort (list);
B. One is not to make any changes to the Sort object class, create the implementation class C of the comparator interface, then put the object into list, then call Collections.sort (list, C);

A.eg

----------------------------------

1  Public classUserImplementsComparable<user>{2     PrivateString name;3     Private intAge ;4     Private intgender;5     PrivateString address;6     7      PublicString GetName () {8         returnname;9     }Ten      One      Public voidsetName (String name) { A          This. Name =name; -     } -      the      Public intGetage () { -         returnAge ; -     } -      +      Public voidSetage (intAge ) { -          This. Age =Age ; +     } A      at      Public intGetgender () { -         returngender; -     } -      -      Public voidSetgender (intgender) { -          This. Gender =gender; in     } -      to      PublicString getaddress () { +         returnaddress; -     } the      *      Public voidsetaddress (String address) { $          This. Address =address;Panax Notoginseng     } -      the      PublicUser () { +         Super(); A     } the      +      PublicUser (String name,intAgeintgender, String address) { -         Super(); $          This. Name =name; $          This. Age =Age ; -          This. Gender =gender; -          This. Address =address; the     } -    Wuyi  the @Override -      Public intcompareTo (User o) { Wu         if(o!=NULL){ -             if( This. Getage () >O.getage ()) { About  $                return1; -}Else if( This. getage () = =O.getage ()) { -  -                return0; A  +             } the  -        } $         return-1; the     } the}

-------------------------------------------Main

1List<user> ulist =NewArraylist<user>();2Ulist.add (NewUser ("Wangbo", 29, 1, "Changsha"));3Ulist.add (NewUser ("Wumei", 44, 1, "Zhuzhou"));4Ulist.add (NewUser ("Zhangjie", 19, 1, "Yueyang"));5Ulist.add (NewUser ("Lihua", 36, 1, "Changsha"));6Ulist.add (NewUser ("Zhangchangzhe", 19, 1, "Hengyang"));7 8 Collections.sort (list);9 Ten  for(User u:ulist) { OneSystem.out.println (U.getname () + "\ T" +u.getage () + "\ T" +u.getaddress ()); A } -  -  

==============================================

B.eg

--------------------------------

1  Public classUser {2     PrivateString name;3     Private intAge ;4     Private intgender;5     PrivateString address;6     7      PublicString GetName () {8         returnname;9     }Ten      One      Public voidsetName (String name) { A          This. Name =name; -     } -      the      Public intGetage () { -         returnAge ; -     } -      +      Public voidSetage (intAge ) { -          This. Age =Age ; +     } A      at      Public intGetgender () { -         returngender; -     } -      -      Public voidSetgender (intgender) { -          This. Gender =gender; in     } -      to      PublicString getaddress () { +         returnaddress; -     } the      *      Public voidsetaddress (String address) { $          This. Address =address;Panax Notoginseng     } -      the      PublicUser () { +         Super(); A     } the      +      PublicUser (String name,intAgeintgender, String address) { -         Super(); $          This. Name =name; $          This. Age =Age ; -          This. Gender =gender; -          This. Address =address; the     } -     Wuyi } the  -  

------------------------------

1 ImportJava.util.Comparator;2 3  Public classComparatorimplImplementsComparator<user>{4 5 @Override6      Public intCompare (user O1, user O2) {7         8         if(NULL!=o1 &&NULL!=O2) {9             if(O1.getage () >O2.getage ()) {Ten                 return1; One}Else if(O1.getage () = =O2.getage ()) { A                 return0; -             } -         } the         return-1; -     } -  -}

-----------------------------------Main

1List<user> ulist =NewArraylist<user>();2Ulist.add (NewUser ("Wangbo", 29, 1, "Changsha"));3Ulist.add (NewUser ("Wumei", 44, 1, "Zhuzhou"));4Ulist.add (NewUser ("Zhangjie", 19, 1, "Yueyang"));5Ulist.add (NewUser ("Lihua", 36, 1, "Changsha"));6Ulist.add (NewUser ("Zhangchangzhe", 19, 1, "Hengyang"));7Collections.sort (Ulist,NewComparatorimpl ());8          for(User u:ulist) {9System.out.println (U.getname () + "\ T" +u.getage () + "\ T" +u.getaddress ());Ten}

Comparable and comparator of a property comparison sort for an object in a Java collection

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.