java 中的對象,正常狀況下,只能進行:== or !=,不能使用> 或<的,(基本數據類型是均可以比較的)
可是實際開發場景中咱們須要對多個對象進行排序,就須要比較對象的大小須要使用兩個接口中的任何一個:
comparable 或者 comparator java
1.1.像String、包裝類實現了Comparable接口,重寫了ComparableTo(obj)方法,給出了比較兩個對象大小的方式ide
1.2..像String、包裝類重寫compareto() 方法之後,進行了從小到大的排列;this
1.3.對於自定義類來講,若是須要排序,咱們可讓自定義類實現compare接口,重寫CompareTo()方法,在compareto()中指明如何排序;
1.4.重寫compareto()的規則:
若是當前對象this 大於形參對象obj,則返回正整數,
若是當前對象this小於形參對象obj,則返回負數;
若是當前對象this小於形參對象obj,則返回0;spa
1 @Test 2 public void test() { 3 String[] arr = new String[]{"AA","DD","BB","KK","JJ"}; 4 Arrays.sort(arr); 5 System.out.println(Arrays.toString(arr)); 6 }
這裏Arrays.sort(arr)以後,自動從小到大排序;緣由就是 String重寫了ComparableTo(obj)方法,指定了排序規則,即從小到大排序;code
排序類:對象
1 package commonmethod.testClass; 2 3 //商品類 4 public class Goods implements Comparable<Goods>{ 5 private Double price; 6 private String name; 7 public Double getPrice() { 8 return price; 9 } 10 public void setPrice(Double price) { 11 this.price = price; 12 } 13 public String getName() { 14 return name; 15 } 16 public void setName(String name) { 17 this.name = name; 18 } 19 public Goods(Double price, String name) { 20 super(); 21 this.price = price; 22 this.name = name; 23 } 24 //指明 商品比較大小的方式,價格從低到高進行排序; 25 @Override 26 public int compareTo(Goods o) { 27 if(this.price>o.price) { 28 return 1; 29 }else if(this.price<o.price) { 30 return -1; 31 } 32 // TODO Auto-generated method stub 33 return 0; 34 } 35 @Override 36 public String toString() { 37 return "Goods [price=" + price + ", name=" + name + "]"; 38 } 39 40 41 }
排序方法體:
blog
1 @Test 2 public void test1() { 3 Goods[] arr = new Goods[4]; 4 arr[0] = new Goods(34.0,"lenovoMouse"); 5 arr[1] = new Goods(43.0,"dellMouse"); 6 arr[2] = new Goods(12.0,"xiaomiMouse"); 7 arr[3] = new Goods(65.0,"huaweiMouse"); 8 Arrays.sort(arr);//Goods實現了Comparale接口,重寫了comparable接口,不然報錯; 9 System.out.println(Arrays.toString(arr)); 10 11 12 }
如下重寫了Comparable接口的compareTo方法;能夠自定義排序規則;
@Override
public int compareTo(Goods o) {
if(this.price>o.price) {
return 1;
}else if(this.price<o.price) {
return -1;
}
// TODO Auto-generated method stub
return 0;
}排序
1.1.背景:
當元素的類型沒有實現comparable接口,並且又不方便修改代碼
或者實現了commpare接口的排序規則不適合當前操做,那麼就須要使用comparator
1.2.重寫compare(object o1,object2 o2)方法,比o1,o2的大小;
若是方法返回正數,則o1>o2,
若是方法返回負數,則o1<o2,
若是方法返回0,則o1=o2,接口
1 @Test 2 public void test1() { 3 Goods[] arr = new Goods[4]; 4 arr[0] = new Goods(34.0,"lenovoMouse"); 5 arr[1] = new Goods(43.0,"dellMouse"); 6 arr[2] = new Goods(12.0,"xiaomiMouse"); 7 arr[3] = new Goods(65.0,"huaweiMouse"); 8 Arrays.sort(arr);//Goods實現了Comparale接口,重寫了comparable接口,不然報錯; 9 System.out.println(Arrays.toString(arr)); 10 11 12 }
這個例子使用comparator 實現:
1 @Test 2 public void test2() { 3 String[] arr = new String[]{"AA","DD","BB","KK","JJ"}; 4 Comparator c = new Comparator() {//匿名子類的非匿名對象; 5 6 @Override 7 public int compare(Object o1, Object o2) { 8 9 return -1; 10 }}; 11 Arrays.sort(arr, c); 12 13 System.out.println(Arrays.toString(arr)); 14 Goods[] goodsArr = new Goods[5]; 15 goodsArr[0] = new Goods(34.0,"lenovoMouse"); 16 goodsArr[1] = new Goods(43.0,"dellMouse"); 17 goodsArr[2] = new Goods(12.0,"xiaomiMouse"); 18 goodsArr[3] = new Goods(65.0,"huaweiMouse"); 19 goodsArr[4] = new Goods(66.0,"huaweiMouse"); 20 //按價格排序 21 Arrays.sort(goodsArr,new Comparator<Goods>() {//匿名對象的匿名子類 22 23 @Override 24 public int compare(Goods o1, Goods o2) { 25 // TODO Auto-generated method stub 26 if(o1.getPrice()>o2.getPrice()) { 27 return -1; 28 }else if(o1.getPrice()<o2.getPrice()) { 29 return 1; 30 }else { 31 return 0; 32 } 33 34 } 35 });//Goods實現了Comparale接口,重寫了comparable接口,不然報錯; 36 System.out.println(Arrays.toString(goodsArr)); 37 38 }
擴展:按名稱排序,若是名稱相同,按價格排序 都是倒敘開發
1 @Test 2 public void test2() { 3 Goods[] goodsArr = new Goods[5]; 4 goodsArr[0] = new Goods(34.0,"lenovoMouse"); 5 goodsArr[1] = new Goods(43.0,"dellMouse"); 6 goodsArr[2] = new Goods(12.0,"xiaomiMouse"); 7 goodsArr[3] = new Goods(65.0,"huaweiMouse"); 8 goodsArr[4] = new Goods(66.0,"huaweiMouse"); 9 10 Arrays.sort(goodsArr,new Comparator<Goods>() {//抽象類中的 匿名對象的匿名子類 11 12 @Override 13 public int compare(Goods o1, Goods o2) { 14 // TODO Auto-generated method stub 15 if(o1.equals(o2.getName())) { 16 if(o1.getPrice()>o2.getPrice()) { 17 return -1; 18 }else if(o1.getPrice()<o2.getPrice()) { 19 return 1; 20 }else { 21 return 0; 22 } 23 }else { 24 return -o1.getName().compareTo(o2.getName()); 25 } 26 27 } 28 });//Goods實現了Comparale接口,重寫了comparable接口,不然報錯; 29 System.out.println(Arrays.toString(goodsArr)); 30 31 }