Two ways to sort Java list (implement comparable interface and Collections.sort overloaded methods)

Source: Internet
Author: User
Tags comparable

Reprinted from: http://blog.csdn.net/zxy_snow/article/details/7232035

There are two ways to sort list by using the Collections.sort method

The first is that the object in the list implements the comparable interface, as follows:

/*** Sort the user according to order*/   Public classUserImplementsComparable<user>{       PrivateString name; PrivateInteger Order;  PublicString GetName () {returnname; }        Public voidsetName (String name) { This. Name =name; }        PublicInteger GetOrder () {returnorder; }        Public voidSetorder (Integer order) { This. Order =order; }        Public intcompareTo (User arg0) {return  This. GetOrder (). CompareTo (Arg0.getorder ()); }} Test: Public classtest{ Public Static voidMain (string[] args) {User user1=NewUser (); User1.setname (A); User1.setorder (1); User User2=NewUser (); User2.setname ("B"); User2.setorder (2); List<User> list =NewArraylist<user>(); //add User2 and add user1 hereList.add (User2);           List.add (user1);           Collections.sort (list);  for(User u:list) {System.out.println (U.getname ()); }       }   }   

The output is as follows
A
B

The second method is implemented according to the Collections.sort overloaded method, for example:

[Java] View plain copy/*** Sort the user according to order*/   Public classUser {//no need to implement comparable interface here    PrivateString name; PrivateInteger Order;  PublicString GetName () {returnname; }        Public voidsetName (String name) { This. Name =name; }        PublicInteger GetOrder () {returnorder; }        Public voidSetorder (Integer order) { This. Order =order; }} This can be written in the main class (hastset-->List-->sort): Public classTest { Public Static voidMain (string[] args) {User user1=NewUser (); User1.setname (A); User1.setprice (11); User User2=NewUser (); User2.setname ("B"); User2.setprice (2); Set<User> Hset =NewHashset<user>();          Hset.add (User2);            Hset.add (user1); List<User> list =NewArraylist<user>();              List.addall (Hset); Collections.sort (list,NewComparator<user>(){               Public intCompare (user arg0, user arg1) {returnArg0.getprice (). CompareTo (Arg1.getprice ());          }          });  for(User u:list) {System.out.println (U.getname ()); }      }  

The output results are as follows:

A
B

The default is ascending, which will.    Return Arg0.getorder (). CompareTo (Arg1.getorder ()); Switch
Return Arg1.getorder (). CompareTo (Arg0.getorder ());
It's in descending order.

Two ways to sort Java list (implement comparable interface and Collections.sort overloaded methods)

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.