[Personal originality] Some Discussions on Object sorting in java (2)

Source: Internet
Author: User

2. Use the Collections. sort () method

The Collections class provides many static methods, such as addAll () and max. When you are dealing with classes in the Collection interface, you can check whether there are any tools you can directly use in this toolbox.

 

Java. util. * List <Person> person = registrant List <Person> person. add (Person ("", 60 person. add (Person ("Large bulb", 24 person. add (Person ("", 24 person. add (Person ("Dalian", 22 person. add (Person ("beijing", 54 Collections. sort (person, Comparator <Person> I = Integer (o2.getAge ()). compareTo (I! = 0 Iterator <Person> it = Person person1 = System. out. println (String. format ("Name: % s, age: % d "}

Running result:

Name: 18th child, age: 60 name: beijing, age: 54 name: 18th child, age: 24 name: Large bulb, age: 24 name: Dalian, age: 22

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.