Ht-7 sorting custom objects in ArrayList

Source: Internet
Author: User
Tags print format sorted by name

1  PackageCom.iotek.set;2 3 Importjava.util.ArrayList;4 Importjava.util.Collections;5 ImportJava.util.Comparator;6 Importjava.util.List;7 /**8  * 9 * Sort the contents of the ArrayList container: Store multiple Person objects (with Name,age,id properties) in ArrayListTen * required to sort by age from small to large, the same age and then sort the output in the natural order of the name One * Ideas: A * Use ArrayList to store the person object, using the static method sort provided by the collections class to - * ArrayList to sort, then output sorted information.  -  * @authorAdministrator the  * -  */ -  Public classCollectionsDemo2 { -     /*1. Create a ArrayList container + * 2. Create a person class with the Name,age,id property - * 3. Sort the data in the container, sort the implementation class of the list interface with the method of the collections class + * 4. Output sorted content*/ A      at      Public Static voidMain (string[] args) { -list<personc> data =NewArraylist<personc>(); -Data.add (NewPersonc ("Jack", 20,10)); -Data.add (NewPersonc ("Rose", 10,7)); -Data.add (NewPersonc ("Mary", 30,6)); -Data.add (NewPersonc ("Zhang", 50,18)); inData.add (NewPersonc ("Jay", 20,11)); -Collections.sort (data,NewComparator<personc>() { to @Override +              Public intCompare (Personc O1, Personc O2) { - //first sort by age. the                 if(O1.getage ()-o2.getage () > 0) { *                     return1; $}Else if(O1.getage ()-o2.getage () < 0) {Panax Notoginseng                     return-1; -}Else{//when the age is equal, it is sorted by name, the                     /*the specific string is an instantiated object of the string class, and you can invoke the String class's + * CompareTo (String anotherstring) method to sort strings in dictionary order A */                    returno1.getname (). CompareTo (O2.getname ()); the                 } +             } -         }); $          $          for(Personc p:data) { - System.out.println (p.tostring ()); -         } the     } - Wuyi } the /*Sort (list<t> list, comparator<? super t> C) Sorts the specified list according to the order produced by the specified comparer - * COMPARATOR< Super t> C C represents a comparer that can be implemented with anonymous inner classes Wu * The anonymous inner class produces an implementation class object of an interface, so implementing the Compare () method in this interface*/ - /*String.CompareTo (String anotherstring) compares two strings in dictionary order, returning a value of type int*/ About  $ classPersonc { -     PrivateString name; -     Private intAge ; -     Private intID; A      PublicPersonc (String name,intAgeintID) { +         Super(); the          This. Name =name; -          This. Age =Age ; $          This. ID =ID; the     } the      PublicString GetName () { the         returnname; the     } -      Public voidsetName (String name) { in          This. Name =name; the     } the      Public intGetage () { About         returnAge ; the     } the      Public voidSetage (intAge ) { the          This. Age =Age ; +     } -      Public intgetId () { the         returnID;Bayi     } the      Public voidSetId (intID) { the          This. ID =ID; -     } - @Override the     //override the ToString method to define the print format the      PublicString toString () { the         return"Person [name=" + name + ", age=" + Age + ", id=" + ID + "]"; the     } -      the}

Ht-7 sorting custom objects in ArrayList

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.