Ordering of lists in Java

Source: Internet
Author: User

The first method is to implement the comparable interface in the list object, and the code is as follows:

 Public classPerson implements Comparable<person> {    PrivateString name; PrivateInteger Order; /** * @return the name*/     PublicString GetName () {returnname; }     /** * @param name * The name to set*/     Public voidsetName (String name) { This. Name =name; }     /** * @return The order*/     PublicInteger GetOrder () {returnorder; }     /** * @param order * The order to set*/     Public voidSetorder (Integer order) { This. Order =order; } @Override Public intcompareTo (person arg0) {return  This. GetOrder (). CompareTo (Arg0.getorder ()); } }

Test examples

 Public Static voidMain (string[] args) {List<Person> ListA =NewArraylist<person>(); Person P1=NewPerson (); Person P2=NewPerson (); Person P3=NewPerson (); P1.setname ("name1"); P1.setorder (1); P2.setname ("name2"); P2.setorder (2); P3.setname ("Name3"); P3.setorder (3);    Lista.add (p2);    Lista.add (p1);    Lista.add (p3);    Collections.sort (ListA);  for(person P:lista) {System. out. println (P.getname ()); }}

The second method is to overload the Collections.sort method with the following code:

 Public classPerson {PrivateString name; PrivateInteger Order; /** * @return the name*/     PublicString GetName () {returnname; }     /** * @param name * The name to set*/     Public voidsetName (String name) { This. Name =name; }     /** * @return The order*/     PublicInteger GetOrder () {returnorder; }     /** * @param order * The order to set*/     Public voidSetorder (Integer order) { This. Order =order; } }
 Public Static voidMain (string[] args) {List<Person> ListA =NewArraylist<person>(); Person P1=NewPerson (); Person P2=NewPerson (); Person P3=NewPerson (); P1.setname ("name1"); P1.setorder (1); P2.setname ("name2"); P2.setorder (2); P3.setname ("Name3"); P3.setorder (3);        Lista.add (p2);        Lista.add (p1);                 Lista.add (p3); Collections.sort (ListA,NewComparator<person>() {             Public intCompare (person arg0, person arg1) {returnArg0.getorder (). CompareTo (Arg1.getorder ());                 }        });  for(person P:lista) {System. out. println (P.getname ()); }    }

The results of two executions are sorted in ascending order according to the order of the person object.

The results of the two executions were: Name1name2name3

Ordering of lists in Java

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.