java中compareTo本來是比較字符串的方法(int類型使用運算符<>=比較)
返回值是整型,它是先比較對應字符的大小(ASCII碼順序),如果第一個字符和參數的第一個字符不等,結束比較,返回他們之間的差值,如果第一個字符和參數的第一個字符相等,則以第二個字符和參數的第二個字符做比較,以此類推,直至比較的字符或被比較的字符有一方結束。
- 如果參數字符串等於此字符串,則返回值 0;
- 如果此字符串小於字符串參數,則返回一個小於 0 的值;
- 如果此字符串大於字符串參數,則返回一個大於 0 的值。
public class Test { public static void main(String args[]) { String str1 = "Strings"; String str2 = "Strings"; String str3 = "Strings123"; int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); result = str3.compareTo( str1 ); System.out.println(result); } }
要在類方法里重寫compareTo方法 可以實現類數組的sort 必須要求類實現Comparable接口(所有繼承collections的都實現了這個接口)
1.重寫compareTo方法
class test implements Comparable<test>{ private String title; private double price; public test(String title,double price){ this.title = title; this.price = price; } @Override public String toString() { return "書名:"+this.title+",價格:"+this.price; } @Override public int compareTo(test o) { //升序 if(this.price > o.price){ return 1; }else if(this.price < o.price){ return -1; }else{ return 0; } }
使用兩者之差作為返回值(類中變量price是double類型 需要返回時強行類型轉換一下)
class test implements Comparable<test>{ private String title; private double price; public test(String title,double price){ this.title = title; this.price = price; } @Override public String toString() { return "書名:"+this.title+",價格:"+this.price; } @Override public int compareTo(test o) { //升序 return (int)(this.price-o.price); // 降序 // return (int)(o.price-this.price); }
主函數測試一下sort函數
我使用的是降序的那個 輸出成功 排序完成
2.構造新的比較器· 實現比較器Comparator 接口
比較器提供的兩個抽象方法 compare以及equals
通過API,我們來解讀接口方法:
Compare()比較用來排序的兩個參數。根據第一個參數小於、等於或大於第二個參數分別返回負整數、零或正整數。 在前面的描述中,符號 sgn(expression) 表示 signum 數學函數,根據 expression 的值為負數、0 還是正數,該函數分別返回 -1、0 或 1。
- 實現程序必須確保對於所有的 x 和 y 而言,都存在 sgn(compare(x, y)) == -sgn(compare(y, x))。(這意味着當且僅當 compare(y, x) 拋出異常時 compare(x, y) 才必須拋出異常。)
- 實現程序還必須確保關系是可傳遞的:((compare(x, y)>0) && (compare(y, z)>0)) 意味着 compare(x, z)>0。
- 最后,實現程序必須確保 compare(x, y)==0 意味着對於所有的 z 而言,都存在 sgn(compare(x, z))==sgn(compare(y, z))。
雖然這種情況很普遍,但並不 嚴格要求 (compare(x, y)==0) == (x.equals(y))。一般說來,任何違背這個條件的 Comparator 都應該清楚地指出這一事實。推薦的語言是“注意:此 Comparator 強行進行與 equals 不一致的排序。”
可能拋出異常ClassCastException - 如果參數的類型不允許此 Comparator 對它們進行比較。
Equals()指示某個其他對象是否“等於”此 Comparator。此方法必須遵守 Object.equals(Object) 的常規協定。此外,僅當 指定的對象也是一個 Comparator,並且強行實施與此 Comparator 相同的排序時,此方法才返回 true。因此,comp1.equals(comp2) 意味着對於每個對象引用 o1 和 o2 而言,都存在 sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2))。
注意,不重寫 Object.equals(Object) 方法總是安全的。然而,在某些情況下,重寫此方法可以允許程序確定兩個不同的 Comparator 是否強行實施了相同的排序,從而提高性能。
覆蓋: 類 Object 中的 equals
參數: obj - 要進行比較的引用對象。
返回: 僅當指定的對象也是一個 Comparator,並且強行實施與此 Comparator 相同的排序時才返回 true。
Arrays.sort()的文檔 (類型數組,比較器)
import java.util.*; import java.util.Arrays; class test //implements Comparable<test> { private String title; private double price; public test(String title, double price) { this.title = title; this.price = price; } @Override public String toString() { return "書名:" + this.title + ",價格:" + this.price; } public int getprice() { return (int)price; } public static void main(String[] args) { test[] arr=new test[4]; test book1=new test("1",100); test book2=new test("2",50); test book3=new test("3",120); test book4=new test("4",300); arr[0]=book1;arr[1]=book2;arr[2]=book3;arr[3]=book4;
// Arrays.sort(arr,new myComparator()); for(test tmp:arr) { System.out.println(tmp.toString()); } return; } }
//類外定義比較器 class myComparator implements Comparator<test>{ public int compare(test t1,test t2) { return (int)(t1.getprice()-t2.getprice()); } }
參考博客:菜鳥教程
https://blog.csdn.net/liuwg1226/article/details/85268814
https://www.cnblogs.com/ldy-blogs/p/8488138.html