Sort Java collection of ArrayList elements

Source: Internet
Author: User

1. New project, New Man class, add property

2. Right-click in the Code editing area, select source, automatically generate the Setter,getter method, and the constructor (similar to the operation, check generate constructor using field ... )

3. Check the corresponding properties, confirm, very convenient.

Results such as:

The Man.java code is as follows:

1  Public classMan {2     3     PrivateString name;4      PublicString GetName () {5         returnname;6     }7      Public voidsetName (String name) {8          This. Name =name;9     }Ten      Public intgetheight () { One         returnheight; A     } -      Public voidSetHeight (intheight) { -          This. Height =height; the     } -     Private intheight; -      -      PublicMan (String name,intheight) { +         Super(); -          This. Name =name; +          This. Height =height; A     } at      -      -  -}

New Test.java

Add code:

Importjava.util.ArrayList; Public classTest { Public Static voidMain (string[] args) {ArrayList<Man> arr=NewArraylist<> ();//new dynamic linked list//Create 4 ObjectsMans man_exam1=NewMan ("Tall", 185); Mans Man_exam2=NewMan ("Middle child", 175); Mans MAN_EXAM3=NewMan ("Shorty", 165); Mans MAN_EXAM4=NewMan ("Tall", 195); //feel free to add to the dynamic arrayArr.add (MAN_EXAM1);        Arr.add (MAN_EXAM4);            Arr.add (MAN_EXAM3); //Print View         for(man Man:arr) {System.out.println (Man.getname ()+man.getheight ()); }                                                    }
View Code

Run the output as follows:

Tall 185
Tall 195
Shorty 165

Can be seen in a disorderly order. Now sort it:

New class: Compare.java

Code:

1 ImportJava.util.Comparator;2 3  Public classCompareImplementscomparator{4 5       Public intCompare (Object Obj0, Object obj1) {6Mans s1=(man) obj0;7Mans s2=(man) obj1;8           9           if(S1.getheight () <=s2.getheight ())Ten                   return1; One                    A           return-1;  -      } -       the}
View Code

Add the following code to the Test.java (the object is already stored in the dynamic array, arr, which is already sequenced after executing the following code):

Compare comparator=new Compare ();
Collections.sort (arr, comparator);

  

Final full version Test.java code:

Importjava.util.ArrayList;Importjava.util.Collections; Public classTest { Public Static voidMain (string[] args) {ArrayList<Man> arr=NewArraylist<> ();//new dynamic linked list//Create 4 ObjectsMans man_exam1=NewMan ("Tall", 185); Mans Man_exam2=NewMan ("Middle child", 175); Mans MAN_EXAM3=NewMan ("Shorty", 165); Mans MAN_EXAM4=NewMan ("Tall", 195); //feel free to add to the dynamic arrayArr.add (MAN_EXAM1);        Arr.add (MAN_EXAM4);            Arr.add (MAN_EXAM3); //Print View         for(man Man:arr) {System.out.println (Man.getname ()+man.getheight ()); } System.out.println ("After sorting"); Compare Comparator=NewCompare ();               Collections.sort (arr, comparator);  for(intI=0;i<arr.size (); i++) {man user_temp=(man) arr.get (i); System.out.println (User_temp.getname ()+","+user_temp.getheight ()); }                                }}
View Code

Project structure:

Sort Java collection of ArrayList elements

Related Article

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.