Use the collections. sort method in the Java set to sort the list.

Source: Internet
Author: User
Use collections. the sort method can sort the 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 classuser implements comparable {privatestring name; privateinteger order; publicstring getname () {returnname;} publicvoid setname (string name) {This. name = Name;} publicinteger getorder () {returnorder;} publicvoid setorder (integer order) {This. order = order;} publicint compareto (User arg0) {returnthi S. getorder (). compareto (arg0.getorder () ;}} test: Public classtest {publicstatic void main (string [] ARGs) {user user1 = new user (); user1.setname (""); user1.setorder (1); User user2 = new user (); user2.setname ("B"); user2.setorder (2); list = new arraylist (); // Add user2 and then add user1 list. add (user2); list. add (user1); collections. sort (list); For (User U: List) {system. out. println (U. getname () ;}} enter The result is as follows AB. The second method is based on collections. the sort overload method is implemented. For example,/*** sorts users by Order */Public classuser {// The comparable interface privatestring name; privateinteger order; publicstring getname () is not required here () {returnname;} publicvoid setname (string name) {This. name = Name;} publicinteger getorder () {returnorder;} publicvoid setorder (integer order) {This. order = order ;}} in the main class: Public classtest {publicstatic void main (string [] ar GS) {user user1 = new user (); user1.setname ("A"); user1.setorder (1); User user2 = new user (); user2.setname ("B "); user2.setorder (2); list = new arraylist (); list. add (user2); list. add (user1); collections. sort (list, newcomparator () {publicint compare (User arg0, user arg1) {returnarg0.getorder (). compareto (arg1.getorder () ;}}); For (User U: List) {system. out. println (U. getname () ;}} the output result is as follows: The structure is simple, but it can only be sorted by fixed attributes. The latter is flexible. You can temporarily specify the sorting items, but the code is not concise enough for multiple fields: view source code printing? Collections. sort (list, newcomparator () {publicint compare (User arg0, user arg1) {// The first professional Inti = arg0.getorder (). compareto (arg1.getorder (); // if the major is the same, perform the second comparison if (I = 0) {// The second comparison intj = arg0.getxxx (). compareto (arg1.getxxx (); // if the educational system is the same, if (j = 0) {returnarg0.getccc () is returned by age (). compareto (arg1.getccc () ;}returnj ;}returni ;}});

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.