Method 1: Implement the Comparator interface and override the compare method
Entity class code:
import java.util.Comparator;/** * Student class method 1* Implement the Comparator interface* and overwrite the compare method* @author liaot * */public class Student implements Comparator<Student>{ private String name; //name private int age; //age/rewrite the comparison method This example is defined as comparing by age @Override public int compare(Student o1, Student o2) { if(o1.getAge() > o2.getAge()){ return 1; }else{ return -1; } } 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; }}Test class:
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Main { public static void main(String[] args) { //Initialize four different students Student stu1 = new Student("Passy A", 20); Student stu2 = new Student("Passy A", 18); Student stu3 = new Student("Passy C", 16); Student stu4 = new Student("Passy C", 19); //Create a new List to add students to List List<Student> stuList = new ArrayList<>(); stuList.add(stu1); stuList.add(stu2); stuList.add(stu3); stuList.add(stu4); System.out.println("Before sorting:======"); for(Student stu :stuList){ System.out.println("Name:"+stu.getName() +" Age"+stu.getAge()); } //Sorting Collections.sort(stuList, stu1); //The first parameter is List The second parameter is an instance of the object System.out.println("Sorted:======"); for(Student stu :stuList){ System.out.println("Name:"+stu.getName() +"Age"+stu.getAge()); } }}Running results:
Method 2: Implement the Comparable interface and override the compareTo method
/** * Student class method 2 implements Comparable interface and rewrites the compareTo method* * @author liaot * */public class Student2 implements Comparable<Student2> { private String name; // name private int age; // age// Rewrite the comparison method This example is defined as comparing by age @Override public int compareTo(Student2 stu) { if (this.age > stu.getAge()) { return 1; } else { return -1; } } public Student2(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; }}Test class
import java.util.ArrayList;import java.util.Collections;import java.util.List;public class Main2 { public static void main(String[] args) { //Initialize four different students Student2 stu1 = new Student2("Passy A", 20); Student2 stu2 = new Student2("Passy A", 18); Student2 stu3 = new Student2("Passy C", 16); Student2 stu4 = new Student2("Passy C", 19); // Create a new List to add students to List List<Student2> stuList = new ArrayList<>(); stuList.add(stu1); stuList.add(stu2); stuList.add(stu3); stuList.add(stu4); System.out.println("Before sorting:======"); for(Student2 stu :stuList){ System.out.println("Name:"+stu.getName() +" Age"+stu.getAge()); } //Sort Collections.sort(stuList); //Only one parameter is List System.out.println("Sorted:======"); for(Student2 stu :stuList){ System.out.println("Name:"+stu.getName() +"Age"+stu.getAge()); } }}Running results
3. Summary: The difference between writing and usage of the two methods:
The above java sorting method according to the attributes of objects in List is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.