Preface
The List interface in the Java.util package inherits the Collection interface and is used to store object collections. Therefore, when sorting these objects, either the object class can compare the same object itself, or use the comparator to perform comparison and sorting.
Student entity class, including name and age attributes, is sorted in ascending order in the name when comparing, and if the names are the same, it is sorted in ascending order in age.
The first type: Comparison of entity classes by themselves
(Implement the comparable interface: public interface Comparable<T> , there is 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.compareTo(o.name); if(flag == 0) { flag = this.age - o.age; } return flag; } } Then use sort(Comparator<? super E> c) method of the List class or sort(List<T> list) of the java.util.Collections tool class (actually, there is only one sentence in it: list.sort(null); ) to sort:
List<Student> students = new ArrayList<Student>(); students.add(new Student("a",10)); students.add(new Student("b",12)); students.add(new Student("b",11)); students.add(new Student("ac",20)); students.sort(null); //Collections.sort(students); result:
a 10 ac 20 b 11 b 12
The second type: sort with the help of a 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> ), which contains int compare(T o1, T o2); and other methods:
Our comparator wants to implement this interface and implement 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; } } When comparing, you can use the sort(Comparator<? super E> c) method of List (or the sort(List<T> list, Comparator<? super T> c) method of the java.util.Collections tool class to sort(List<T> list, Comparator<? super T> c) method) to sort.
List<Student> students = new ArrayList<Student>(); students.add(new Student("a",10)); students.add(new Student("b",12)); students.add(new Student("b",11)); students.add(new Student("ac",20)); 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:
a 10 ac 20 b 11 b 12
Summarize
The above is all about sorting List in Java. I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate.