Http://www.verejava.com/?id=169931036202101
/** Knowledge Points: Comparative class Comparator title: A class of students according to mathematical results from small to large order of thought: 1. Abstract class: 1.1 Class (Classset) 1.2 Students (St Udent) 2. Find out the class relationship: 2.1 Students belong to class Student-Classset (many to 1) 3. Find class Properties: 3.1 Classset (class name, class number) 3.2 Student (student name, math score) 4. Find class Methods: 4.1 Students added to class classset{addstudent (Student s)} 4.2 student scores from small to large sort Classset{sortbyscore ()}*/import J Ava.util.arrays;import Java.util.comparator;public class testcomparator{public static void Main (string[] args) { Instantiate 4G class Classset c=new classset ("4G", 4); Add Student C.addstudent (new Student ("Li Ming", 90)); C.addstudent (New Student ("Li Hao", 80)); C.addstudent (New Student ("Wang Tao", 95)); C.addstudent (New Student ("Zhang Shengliang", 70)); Get 4G class student array set student[] students=c.getstudents (); Output student information for (Student s:students) {if (s!=null) System.out.println (s.getname () + "," + S.getmathscore ()); } System.out.println ("\ n in ascending order of students ' grades"); Arrays.sort (Students,new studentasccomparator ()); for (Student s:students) {if (s!=null) System.out.println (s.getname () + "," +s.getmathscore () ); } System.out.println ("\ n Sort by student grade"); Arrays.sort (Students,new studentdesccomparator ()); for (Student s:students) {if (s!=null) System.out.println (s.getname () + "," +s.getmathscore () ); }}}class classset{Private String classname;//class name private int maxsize;//class student number private int currentsize;//current Multi Less student private student[] students;//array public classset (String classname,int maxSize) for all students {THIS.CLASSNAME=CL Assname; This.maxsize=maxsize; Students=new Student[maxsize]; } public student[] Getstudents () {return this.students; }/** Add student */public void addstudent (Student s) {for (int i=0;i<students.length;i++) { if (students[i]==null) {students[i]=s; currentsize++; Break }}}}class Student {private String name;//student name private int mathscore;//Math score Public Student (String Name,int mathscore) {this.name=name; This.mathscore=mathscore; } public String GetName () {return this.name; } public void SetName (String name) {this.name=name; } public int Getmathscore () {return this.mathscore; } public void Setmathscore (int mathscore) {this.mathscore=mathscore; }}/** students in ascending order */class Studentasccomparator implements comparator{public int compare (Object o1,object O2) { if ((O1 instanceof Student) && (O2 instanceof Student)) {Student s1= (Student) O1; Student s2= (Student) O2; if (S1.getmathscore () >s2.getmathscore ()) return 1; if (S1.getmatHscore () <s2.getmathscore ()) return-1; } return 0; }}/** students in descending order */class Studentdesccomparator implements comparator{public int compare (Object o1,object O2) { if ((O1 instanceof Student) && (O2 instanceof Student)) {Student s1= (Student) O1; Student s2= (Student) O2; if (S1.getmathscore () >s2.getmathscore ()) return-1; if (S1.getmathscore () <s2.getmathscore ()) return 1; } return 0; }}
Http://www.verejava.com/?id=169931036202101
Java Common class Library comparison interface Comparator