Objective
The list interface in the Java.util package inherits the collection interface and is used to store objects, so when sorting these objects, either let the object class implement the comparison of the same objects themselves, or use the comparator to compare the sort.
Student entity class that contains the name and age attributes, sorted by name in ascending order, and sorted by age if the name is the same.
The first type: entity classes to implement their own comparison
(Implement comparable interface: public interface Comparable<T> , inside on a method declaration: public int compareTo(T o); )
Sample code:
public class Student implements comparable<student>{
private String name;
private int age;
Public Student () {
super ();
TODO auto-generated constructor stub
} public
Student (String name, int age) {
super ();
this.name = name;
This.age = age;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
@Override public
int compareTo (Student o) {
//TODO auto-generated method stub
int flag = This.name.compar ETo (o.name);
if (flag = = 0) {
flag = this.age-o.age;
}
return flag;
}
Then use the list class sort(Comparator<? super E> c) method or java.util.Collections tool class sort(List<T> list) (in fact, in a sentence: list.sort(null); ) to sort:
list<student> students = new arraylist<student> ();
Students.add (New Student ("a",));
Students.add (New Student ("B",));
Students.add (New Student ("B", one));
Students.add (New Student ("AC");
Students.sort (NULL);
Results:
Second: Sort by comparator.
Sample code:
public class Student {
private String name;
private int age;
Public Student () {
super ();
TODO auto-generated constructor stub
} public
Student (String name, int age) {
super ();
this.name = name;
This.age = age;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
The comparator java.util.Comparator class is an interface ( public interface Comparator<T> ), containing int compare(T o1, T o2); methods such as:
Our comparer implements the interface and implements the compare method:
Private class Studentcomparator implements comparator<student> {
@Override public
int compare (Student O1 , Student O2) {
//TODO auto-generated method stub
int flag = O1.getname (). CompareTo (O2.getname ());
if (flag = = 0) {
flag = o1.getage ()-o2.getage ();
}
return flag;
}
You can sort by using the list sort(Comparator<? super E> c) method (or java.util.Collections the method of the tool class) when you compare it sort(List<T> list, Comparator<? super T> c) .
list<student> students = new arraylist<student> ();
Students.add (New Student ("a",));
Students.add (New Student ("B",));
Students.add (New Student ("B", one));
Students.add (New Student ("AC");
Test T = new test ();
Students.sort (T.new studentcomparator ());
Collections.sort (Students, T.new studentcomparator ());
for (Student student:students) {
System.out.println (student.getname () + "" +student.getage ());
The result is the same as the first method:
Summarize
The above is about the list in Java all the contents of the order, I hope this article on the content of everyone's study or work can bring certain help, if you have questions you can message exchange.