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);