http://www.verejava.com/?id=169930999133100
/** Knowledge Points: Comparative class comparable 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;public class testcomparable{public static void Main (string[] args) {//Instantiate 4G class Classs ET 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 ("According to the students ' grades "); C.sortbybuble (); Arrays.sort (students); C.sortbymerge (); 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 }}}/** sort from small to large according to student's math score */public student[] Sortbybuble () {for (iNT i=0;i<currentsize-1;i++) {for (int j=0;j<currentsize-i-1;j++) {Stude NT S=STUDENTS[J]; if (Students[j].getmathscore () >students[j+1].getmathscore ()) {students[j]=students[j+ 1]; Students[j+1]=s; }}} return this.students; } public void Sortbymerge () {//Create a temporary array to hold the partition element comparable[] Temparray=students.clone (); Merge_sort (students,temparray,0,students.length); } private void Merge_sort (comparable[] array,comparable[] temparray, int first, int last) {if (first+1<last) {int mid= (first+last)/2; To the left half partial sorts merge_sort (Array,temparray,first,mid); To have half-part sort merge_sort (array,temparray,mid,last); Merge to a temporary array, and then copy to the array merge (Array,temparray,first,mid,last); }} private void Merge (ComparablE[] array,comparable[] temparray, int first, int mid,int last) {int beginleft=first; int beginright=mid; int K=first; while ((Beginleft<mid) && (beginright<last)) {if (Array[beginleft].compareto (Array[beginright]) <0) {Temparray[k]=array[beginleft]; beginleft++; }else{Temparray[k]=array[beginright]; beginright++; } k++; } while (Beginleft<mid) {temparray[k++]=array[beginleft++]; } while (Beginright<last) {temparray[k++]=array[beginright++]; } for (int i=first;i<last;i++) {array[i]=temparray[i]; }}}class Student implements comparable{private String name;//student name private int mathscore;//Math score Public Studen T (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; The/** implements the comparable interface to replicate the CompareTo (T O) method if the small to large sort is greater than the return 1 is less than the 1 equals returns 0 if the sort from large to small is greater than the return-1 is less than 1 equals return 0 */Publ IC int compareTo (Object obj) {if (obj instanceof Student) {Student s= (Student) obj; if (This.mathscore>s.getmathscore ()) return 1; if (This.mathscore<s.getmathscore ()) return-1; } return 0; } }
http://www.verejava.com/?id=169930999133100
Comparison class comparable of Java Common class library