JAVA中Arrays.sort()使用兩種方式(Comparable和Comparator介面)對對象或者引用進行排序

來源:互聯網
上載者:User

標籤:java   arrays.sort   comparable   comparator   對象或引用的排序   

一、描述

自訂的類要按照一定的方式進行排序,比如一個Person類要按照年齡進行從小到大排序,比如一個Student類要按照成績進行由高到低排序。

這裡我們採用兩種方式,一種是使用Comparable介面:讓待排序對象所在的類實現Comparable介面,並重寫Comparable介面中的compareTo()方法,缺點是只能按照一種規則排序。

另一種方式是使用Comparator介面:編寫多個排序方式類實現Comparator介面,並重寫新Comparator介面中的compare()方法,在調用Arrays的sort()時將排序類對象作為參數傳入:public static <T> void sort(T[] a,Comparator<? super T> c),根據指定比較子產生的順序對指定對象數組進行排序。數組中的所有元素都必須是通過指定比較子可相互比較的(也就是說,對於數組中的任何 e1 和 e2 元素而言,c.compare(e1, e2) 不得拋出 ClassCastException)。

優點是可以按照多種方式排序,你要按照什麼方式排序,就建立一個實現Comparator介面的排序方式類,然後將該排序類的對象傳入到Arrays.sort(待排序對象,該排序方式類的對象)


二、原始碼

方式1:使用Comparable介面

package tong.day4_27.systemUse;import java.util.Arrays;/** * 使用Comparable介面:讓待排序對象所在的類實現Comparable介面,並重寫Comparable介面中的compareTo()方法 * 缺點是只能按照一種規則排序 * @author tong * */public class ObjectSort {public static void main(String[] args) {Person[] persons = new Person[5];persons[0] =new Person("tom",45);persons[1] =new Person("jack",12);persons[2] =new Person("bill",21);persons[3] =new Person("kandy",34);persons[4] =new Person();Arrays.sort(persons);for (Person person:persons) {System.out.println(person);}}}class Person implements Comparable<Person>{private String name;private int age;public Person(String name,int age){this.name = name;this.age = age;}public Person(){this("unknown", 0);}//重寫該類的compareTo()方法,使其按照從小到大順序排序@Overridepublic int compareTo(Person o) { return age-o.age;}//重寫Student類的toString()方法,在輸入對象時按照以下方式輸出@Overridepublic String toString() {return "Person[name:"+name+",age:"+age+"]";}}
運行結果:


方式2:使用Comparator介面

package tong.day4_27.systemUse;import java.util.Arrays;import java.util.Comparator;/** * 使用Comparator介面:編寫多個排序方式類實現Comparator介面,並重寫新Comparator介面中的compare()方法 * public static <T> void sort(T[] a,Comparator<? super T> c),根據指定比較子產生的順序對指定對象數組進行排序。數組中的所有元素都必須是通過指定比較子可相互比較的 * (也就是說,對於數組中的任何 e1 和 e2 元素而言,c.compare(e1, e2) 不得拋出 ClassCastException)。 * 優點是可以按照多種方式排序,你要按照什麼方式排序,就建立一個實現Comparator介面的排序方式類,然後將該排序類的對象傳入到Arrays.sort(待排序對象,該排序方式類的對象) * @author tong * */public class ComparatorUse {public static void main(String[] args) {Student[] persons = new Student[5];persons[0] =new Student("tom",1,88,45);persons[1] =new Student("jack",6,80,12);persons[2] =new Student("bill",4,68,21);persons[3] =new Student("kandy",2,98,34);persons[4] =new Student("lily",5,94,20);System.out.println("排序前的資料:");for (Student student:persons) {System.out.println(student);}//建立SortByNumber對象,將其作為參數傳入Arrays.sort(persons,sortByNumber)方法中SortByNumber sortByNumber = new SortByNumber();Arrays.sort(persons,sortByNumber);System.out.println("根據學生編號由低到高排序:");for (Student student:persons) {System.out.println(student);}SortByScore sortByScore = new SortByScore();Arrays.sort(persons,sortByScore);System.out.println("根據學產生績由高到低排序:");for (Student student:persons) {System.out.println(student);}}}class Student {private String name;private int number;private int score;private int age;public Student(String name,int number,int score,int age){this.name = name;this.number = number;this.score = score;this.age = age;}//重寫Student類的toString()方法,在輸入對象時按照以下方式輸出@Overridepublic String toString() {return "Student[name:"+name+",age:"+age+",number:"+number+",score:"+score+"]";}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}//按照學號由低到高排列,建立SortByNumber類,該類實現Comparator,重寫該介面的compare()class SortByNumber implements Comparator<Student>{//重寫該介面的compare()使其按照學號由小到大排序(前者減去後者)@Overridepublic int compare(Student o1, Student o2) {return o1.getNumber()-o2.getNumber();}}//按照分數由高到低排列,建立SortByScore類,該類實現Comparator,重寫該介面的compare()class SortByScore implements Comparator<Student>{//重寫該介面的compare()使其按照分數由高到低排序(後者減去前者)@Overridepublic int compare(Student o1, Student o2) {return o2.getScore()-o1.getScore();}}
運行結果:




JAVA中Arrays.sort()使用兩種方式(Comparable和Comparator介面)對對象或者引用進行排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.