Java Tutorial Two examples of ArrayList ordering sharing _java

Source: Internet
Author: User
Tags sorted by name

Example 1

Copy Code code as follows:

Package com.yonyou.test;
Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;
Import java.util.List;
public class Test {
public static void Main (string[] args) {
Student ZLJ = new Student ("Dingxiaoyu", 21);
Student DXY = new Student ("Zhao Si", 22);
Student CJC = new Student ("John", 11);
Student LGC = new Student ("American", 19);
list<student> studentlist = new arraylist<student> ();
Studentlist.add (ZLJ);
Studentlist.add (DXY);
Studentlist.add (CJC);
Studentlist.add (LGC);
System.out.println ("Sorted according to Age:");
Collections.sort (Studentlist, New Sortbyage ());
for (Student student:studentlist) {
System.out.println (Student.getname () + "/" + student.getage ());
}
System.out.println ("=========");
System.out.println ("Sorted by name");
Collections.sort (Studentlist, New Sortbyname ());
for (Student student:studentlist) {
System.out.println (Student.getname () + "/" + student.getage ());
}
}
}
Class Sortbyage implements Comparator {
public int Compare (object O1, Object O2) {
Student S1 = (Student) O1;
Student s2 = (Student) O2;
if (S1.getage () > S2.getage ())
return 1;
else if (s1.getage () = = S2.getage ()) {
return 0;
}
return-1;
}
}
Class Sortbyname implements Comparator {
public int Compare (object O1, Object O2) {
Student S1 = (Student) O1;
Student s2 = (Student) O2;
if (S1.getname (). CompareTo (S2.getname ()) < 0)
return-1;
else if (S1.getname (). CompareTo (S2.getname ()) > 0) {
return 1;
}
return 0;
}
}
Class student{
private int age;
private String name;
public int getage () {
return age;
}
public void Setage (int age) {
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public Student (String Name,int age) {
This.age = age;
THIS.name = name;
}
}

Example 2

The problem of array ordering is often encountered. For example, I have a person class whose instance objects are stored in the ArrayList array, and now I want to sort the person objects in the ArrayList array by age.
In fact, this situation is often encountered.
The source code is given below:

1:person.java file:

Copy Code code as follows:

public class person{
String name;
int age;

Public person (String Name,int age) {
THIS.name = name;
This.age = age;

}
public int getage () {
return age;
}
public void Setage (int age) {
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
}


2:mycomparator.java

Copy Code code as follows:

To implement the comparator interface, which is to define the collation, you can define almost any rule
Package Com.infoearth;
Import java.util.*;
public class Mycomparator implements comparator{

public int Compare (Object o1,object O2) {
Person p1= (person) O1;
Person p2= (person) O2;
if (p1.age<p2.age)
return 1;
Else
return 0;
}

}

3:listsort.java

Copy Code code as follows:

Package Com.infoearth;

Import java.util.ArrayList;
Import java.util.Collections;
Import Java.util.Comparator;

public class Listsort {
public static void Main (string[] args) {
ArrayList list = new ArrayList ();
List.add (New person ("LCL", 28));
List.add (New person ("FX", 23));
List.add (New person ("WQX", 29));
Comparator comp = new Mycomparator ();
Collections.sort (List,comp);
for (int i = 0;i<list.size (); i++) {
Person P = (person) list.get (i);
System.out.println (P.getname ());
}

}

}

Of course, if your list is wrapped in a base type or string, just Collections.sort (list);

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.