Sorting methods for list in Java

Source: Internet
Author: User

Method One: Implement the comparable interface

 Packagecom.java.test; Public classPersonImplementsComparable<person> {    PrivateString name; PrivateInteger age;  PublicPerson () {} PublicPerson (String name, Integer age) { This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; } @Override Public intcompareTo (Person o) {returnAge.compareto (O.getage ());//Positive order
returno.getage (). CompareTo (age);//Reverse
} }
 Packagecom.java.test;Importjava.util.ArrayList;Importjava.util.Collections;Importjava.util.List; Public classPersontest { Public Static voidMain (string[] args) {List<Person> list =NewArraylist<person>(); List.add (NewPerson ("E", 16)); List.add (NewPerson ("C", 14)); List.add (NewPerson ("A", 12)); List.add (NewPerson ("B", 13)); List.add (NewPerson ("D", 15));  for(person person:list) {System.out.println (Person.getage ()); } System.out.println ("####################");        Collections.sort (list);  for(person person:list) {System.out.println (Person.getage ()); }    }}

Method Two: Call Collections.sort (list<t> List, comparator<? super T> C), define Comparator

 Packagecom.java.test; Public classPerson {PrivateString name; PrivateInteger age;  PublicPerson () {} PublicPerson (String name, Integer age) { This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicInteger getage () {returnAge ; }     Public voidsetage (Integer age) { This. Age =Age ; }}
 Packagecom.java.test;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List; Public classPersontest { Public Static voidMain (string[] args) {List<Person> list =NewArraylist<person>(); List.add (NewPerson ("E", 16)); List.add (NewPerson ("C", 14)); List.add (NewPerson ("A", 12)); List.add (NewPerson ("B", 13)); List.add (NewPerson ("D", 15));  for(person person:list) {System.out.println (Person.getage ()); } System.out.println ("####################"); Collections.sort (list,NewComparator<person>() {@Override Public intCompare (person O1, person O2) {returno1.getage (). CompareTo (O2.getage ());//Positive order
return o2.getage (). CompareTo (O1.getage ());//Reverse }});
for(person person:list) {System.out.println (Person.getage ()); } }}

Sorting methods for list 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.