How do I save an object in a collection, and sort by properties in the object?

Source: Internet
Author: User



Package Testproject;import Java.util.arraylist;import Java.util.collections;import java.util.comparator;public Class Testpro {public static void main (string[] args) {arraylist<testpro> zslist = new arraylist<testpro> (); z Slist.add (New Testpro ("1", "0000")), Zslist.add (New Testpro ("Y", "1111")), Zslist.add (New Testpro ("Y", "1111444")); Zslist.add (New Testpro ("I Am a hot man", "2222")), Zslist.add (New Testpro ("S", "3333")), Zslist.add (New Testpro ("A", "4444")) Zslist.add (New Testpro ("I", "5555")), Zslist.add (New Testpro ("V", "6666")), Zslist.add (New Testpro ("Q", "7777")); Zslist.add (New Testpro ("I", "8888")); Collections.sort (Zslist, New comparator<testpro> () {@Overridepublic int compare (Testpro O1, Testpro O2) {//TODO Au To-generated method Stubreturn o1.getabbreviation (). CompareTo (O2.getabbreviation ());}); for (int i = 0; i < zslist.size (); i++) {System.out.println (Zslist.get (i). Getabbreviation () + "= = =" + Zslist.get (i). Getn Ame ());}} private string Abbreviation;private string Name;puBlic Testpro () {super ();} Public Testpro (string abbreviation, string name) {super (); this.abbreviation = Abbreviation;this.name = name;} Public String getabbreviation () {return abbreviation;} public void Setabbreviation (String abbreviation) {this.abbreviation = abbreviation;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How do I save an object in a collection, and sort by properties in the object?

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.