Java list sorting collections. Sort ()

Source: Internet
Author: User

You can use the collections. sort method to sort a list in two ways.

The first method is to implement the comparable interface for objects in the list, as follows:

/*** Sort users by Order */public class user implements comparable <user> {private string name; private integer order; Public String getname () {return name ;} public void setname (string name) {This. name = Name;} public integer getorder () {return order;} public void setorder (integer order) {This. order = order;} public int compareto (User arg0) {return this. getorder (). compareto (arg0.getorder () ;}} test: public class test {public static void main (string [] ARGs) {user user1 = new user (); user1.setname ("A"); user1.setorder (1); User user2 = new user (); user2.setname ("B"); user2.setorder (2 ); list <user> List = new arraylist <user> (); // Add user2 and then add user1 list. add (user2); list. add (user1); collections. sort (list); For (User U: List) {system. out. println (U. getname ());}}}


The output result is as follows:
A
B

The second method is implemented based on the collections. Sort overload method, for example:

/*** Sort users by Order */public class user {// you do not need to implement the comparable interface private string name; private integer order; Public String getname () {return name ;} public void setname (string name) {This. name = Name;} public integer getorder () {return order;} public void setorder (integer order) {This. order = order ;}} in the main class, you can write (hastset --> list --> sort for sorting): Public class test {public static void main (string [] ARGs) {user user1 = new user (); user1.setname ("A"); user1.setprice (11); User user2 = new user (); user2.setname ("B "); user2.setprice (2); set <user> hset = new hashset <user> (); hset. add (user2); hset. add (user1); List <user> List = new arraylist <user> (); list. addall (hset); collections. sort (list, new comparator <user> () {public int compare (User arg0, user arg1) {return arg0.getprice (). compareto (arg1.getprice () ;}}); For (User U: List) {system. out. println (U. getname ());}}

The output result is as follows:

A
B


The default value is ascending. Return arg0.getorder (). compareto (arg1.getorder (); changed:
Return arg1.getorder (). compareto (arg0.getorder ());
In descending order.

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.