java 比较器
一.通过Comparable 接口,实现自然排序
import java.lang.reflect.Array; import java.util.Arrays; public class ComparableTest { public static void main(String[] args) { // 重写Comparable接口的规则 String [] arr = new String[]{"BB","ff","de","AA"}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); // Goods[] goods = new Goods[4]; goods[0] = new Goods("huaweimouse",45.9); goods[1] = new Goods("lemouse",45.1); goods[2] = new Goods("xiaomimouse",48.9); goods[3] = new Goods("luojimouse",100.5); Arrays.sort(goods); System.out.println(Arrays.toString(goods)); } } // 1.实现Comparable接口类 class Goods implements Comparable { private String name; private Double price; public Goods(String name,Double price){ this.name =name; this.price =price; } public void setName(String name) { this.name = name; } public void setPrice(Double price) { this.price = price; } public String getName() { return name; } public Double getPrice() { return price; } public String toString(){ return "Goods"+"["+this.name+","+this.price+"]"; } // 2.重写compareTo 方法,指明商品比较大小的方法 @Override public int compareTo(Object o) { if (o instanceof Goods ){ Goods goods = (Goods)o; if (this.pricegoods.price){ return 1; }else{ return 0; } // // 方式二 // return Double.compare(this.price,goods.price); }else{ throw new RuntimeException("传入的数据类型不一致!"); } } }
二.通过Comparator接口,实现定制排序
import java.lang.reflect.Array; import java.util.Arrays; import java.util.Comparator; import org.junit.Test; public class ComparableTest { public static void main(String[] args) { // 重写Comparable接口的规则 // String[] arr = new String[]{"BB", "ff", "de", "AA"}; String [] arr = new String[]{"BB","FF","DD","AA"}; // Arrays.sort(arr); // System.out.println(Arrays.toString(arr)); // Goods[] goods = new Goods[4]; goods[0] = new Goods("huaweimouse", 45.9); goods[1] = new Goods("lemouse", 45.1); goods[2] = new Goods("xiaomimouse", 48.9); goods[3] = new Goods("luojimouse", 100.5); // Arrays.sort(goods); // System.out.println(Arrays.toString(goods)); } @Test public void testGood(){ Goods[] goods = new Goods[4]; goods[0] = new Goods("huaweimouse", 45.9); goods[1] = new Goods("lemouse", 45.1); goods[2] = new Goods("xiaomimouse", 48.9); goods[3] = new Goods("luojimouse", 100.5); Arrays.sort(goods,new Comparator(){ @Override public int compare(Object o1, Object o2) { if (o1 instanceof Goods && o2 instanceof Goods){ Goods s1 =(Goods)o1; Goods s2 =(Goods)o2; // return Double.compare(s1.getPrice(),s2.getPrice());// 从小到大排 return -Double.compare(s1.getPrice(),s2.getPrice());// 从大到小排 // }else{ throw new RuntimeException("输入的数据类型有误"); } } }); System.out.println(Arrays.toString(goods)); } } class TestComparator{ } // 1.实现Comparable接口类 class Goods implements Comparable { private String name; private Double price; public Goods(String name,Double price){ this.name =name; this.price =price; } public void setName(String name) { this.name = name; } public void setPrice(Double price) { this.price = price; } public String getName() { return name; } public Double getPrice() { return price; } public String toString(){ return "Goods"+"["+this.name+","+this.price+"]"; } // 2.重写compareTo 方法,指明商品比较大小的方法 @Override public int compareTo(Object o) { if (o instanceof Goods ){ Goods goods = (Goods)o; if (this.pricegoods.price){ return 1; }else{ return 0; } // // 方式二 // return Double.compare(this.price,goods.price); }else{ throw new RuntimeException("传入的数据类型不一致!"); } } }