Homework is given to students together, and multiple entities are sorted by their attributes (just after learning object-oriented, the interface written is not used, just for students to learn how to use it):
/** * @comment:Student Information * @author Fourier series * @date 2018 October 22, 2001, 2:18:14 p.m. */ public class Student { private String name; private int age; private int height; private int mathScore; private int engScore; private String schName; 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; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getMathScore() { return mathScore; } public void setMathScore(int mathScore) { this.mathScore = mathScore; } public int getEngScore() { return engScore; } public void setEngScore(int engScore) { this.engScore = engScore; } public String getSchName() { return schName; } public void setSchName(String schName) { this.schName = schName; } @Override public String toString() { return "Full name: "+this.name+" ,Age: "+this.age+" ,Height:"+this.height+" ,School:"+this.schName+ " ,Mathematics Achievements:"+this.mathScore+" ,English Achievements:"+this.engScore; } }
/** * @comment:Sorting Method Interface * @author Fourier series * @date 2018 October 22, 2001, 3:46:53 p.m. */ public interface StuSortInt { /** * @comment:Sorting of Student Information * @author:Fourier series * @date:2018 22 October 2000, 2:40:11 p.m. * @param list */ public void compareStudent(List<Student> list); /** * @comment:Multiple Sorting of Student Information * @author:Fourier series * @date:2018 October 22, 2001, 3:28:19 p.m. * @param list */ public void compareStudentInfo(List<Student> list); }
/** *@comment:Implementation class of interface *@author Fourier series *@date 2018 October 22, 2001, 3:28:41 p.m. */ public class StuSortImpl implements StuSortInt{ /** * @comment:Students'information is sorted by age * @author Fourier series * @date 2018 October 22, 2001, 3:26:43 p.m. */ @Override public void compareStudent(List<Student> list) { Comparator<Student> comparator = new Comparator<Student>(){ @Override public int compare(Student st1, Student st2) { return st2.getAge()-st1.getAge(); } }; Collections.sort(list, comparator); for (Student student : list) { System.out.println(student.toString()); } } /** * @comment:Multiple Sorting of Student Information * @author Fourier series * @date 2018 October 22, 2001, 3:28:50 p.m. */ @Override public void compareStudentInfo(List<Student> list) { Comparator<Student> comparator = new Comparator<Student>(){ @Override public int compare(Student st1, Student st2) { // Rewrite compare method if(st2.getAge()!=st1.getAge()){ // According to age standard return st2.getAge()-st1.getAge(); }else if(!st2.getName().equals(st1.getName())){ // The same age, according to the name standard return st2.getName().compareTo(st1.getName()); }else{ return st2.getMathScore()-st1.getMathScore(); // According to Mathematics Achievement Standard } } }; // Call Collection's sort method for sorting Collections.sort(list, comparator); // ergodic for (Student student : list) { System.out.println(student.toString()); } } }
/** * @comment:Student Information Management * @author Fourier series * @date 2018 October 22, 2001, 2:19:10 p.m. */ public class StudentController { public static void main(String[] args) { /** * Student Information Making Data */ Student stu = new Student(); Student stu1 = new Student(); Student stu2 = new Student(); Student stu3 = new Student(); stu.setName("Ye Liang Chen"); stu.setAge(22); stu.setSchName("Lan Xiang"); stu.setHeight(171); stu.setMathScore(85); stu.setEngScore(88); stu1.setName("Zhao Tian Tian"); stu1.setAge(23); stu1.setSchName("Green Xiang"); stu1.setHeight(176); stu1.setMathScore(99); stu1.setEngScore(77); stu2.setName("Ye Liang Chen"); stu2.setAge(28); stu2.setSchName("Hung Xiang"); stu2.setHeight(180); stu2.setMathScore(99); stu2.setEngScore(99); stu3.setName("Uncle of Science"); stu3.setAge(23); stu3.setSchName("Luo Xiang"); stu3.setHeight(198); stu3.setMathScore(23); stu3.setEngScore(32); List<Student> list = new ArrayList<>(); list.add(stu); list.add(stu1); list.add(stu2); list.add(stu3); StuSortImpl stuSortImpl = new StuSortImpl(); //1. Sort by age stuSortImpl.compareStudent(list); System.out.println("Sort by multiple criteria:"); //2. Multiple standards stuSortImpl.compareStudentInfo(list); } }
Result:
Name: Ye Liangchen, Age: 28, Height: 180, School: Hongxiang, Mathematics Achievement: 99, English Achievement: 99 Name: Zhao Ritian, age: 23, height: 176, school: Lvxiang, math score: 99, English score: 77 Name: Uncle Ke, age: 23, height: 198, school: Luoxiang, math score: 23, English score: 32 Name: Ye Liangchen, Age: 22, Height: 171, School: Lanxiang, Mathematics Achievement: 85, English Achievement: 88 Sort by multiple criteria: Name: Ye Liangchen, Age: 28, Height: 180, School: Hongxiang, Mathematics Achievement: 99, English Achievement: 99 Name: Zhao Ritian, age: 23, height: 176, school: Lvxiang, math score: 99, English score: 77 Name: Uncle Ke, age: 23, height: 198, school: Luoxiang, math score: 23, English score: 32 Name: Ye Liangchen, Age: 22, Height: 171, School: Lanxiang, Mathematics Achievement: 85, English Achievement: 88