但在开发场景中,我们需要对多个对象进行排序,即需要比较对象的大小。
如何实现:需要两个接口中的任意一个:Comparable 或 Comparator
@Test public void test(){ String[] str = new String[]{"CC","JJ","MM","DD","GG"}; Arrays.sort(str); System.out.println(Arrays.toString(str)); // [CC, DD, GG, JJ, MM] }
① 如果当前对象this大于形参对象obj,则返回正整数;
② 如果当前对象this小于形参对象obj,则返回负整数;
③ 如果当前对象this等于形参对象obj,则返回 0
对于自定义类来说,如果需要排序,我们可以让自定义类实现Comparable接口,重写
compareTo(Object obj)方法,在该方法中致命如何排序
public class CompareTest { @Test public void test2(){ Goods[] str = new Goods[5]; str[0] = new Goods("lenovo",19); str[1] = new Goods("huawei",69); str[2] = new Goods("xiaomi",49); str[3] = new Goods("huawei",99); str[4] = new Goods("dell",99); Arrays.sort(str); System.out.println(Arrays.toString(str)); // [{name='lenovo', price=19.0}, {name='xiaomi', price=49.0}, {name='huawei', price=69.0}, // {name='dell', price=99.0}, {name='huawei', price=99.0}] } } class Goods implements Comparable{ private String name; private double price; public Goods() { } public Goods(String name, double price){ this.name = name; this.price = price; } @Override public String toString() { return "{" + "name='" + name + '\'' + ", price=" + price + '}'; } @Override public int compareTo(Object o) { System.out.println("-----------------"); if (o instanceof Goods){ Goods goods = (Goods)o; if (this.price > goods.price) return 1; else if(this.price < goods.price) return -1; else return this.name.compareTo(goods.name);//将name属性也进行了排名 } throw new RuntimeException("传入的数据类型不正确"); } }
当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码,或者实现了java.lang.Comparable接口的排序规则不适合当前的额操作,那么就可以考虑而用Comparator的对象来排序,强行对多个对象进行整体排序的比较
```java @Test public void test3(){ Goods[] str1 = new Goods[6]; str1[0] = new Goods("lenovo",18); str1[1] = new Goods("huawei",68); str1[2] = new Goods("xiaomi",48); str1[3] = new Goods("huawei",98); str1[4] = new Goods("dell",98); str1[5] = new Goods("oppo",298); Arrays.sort(str1, new Comparator() { @Override public int compare(Object o1, Object o2) { if (o1 instanceof Goods && o2 instanceof Goods){ Goods g1 = (Goods) o1; Goods g2 = (Goods) o2; //按照名字从大到小排 if (g1.getName().equals(g2.getName())){ return -Double.compare(g1.getPrice(),g2.getPrice()); }else return -g1.getName().compareTo(g2.getName()); } throw new RuntimeException("输入数据结构异常"); } }); System.out.println(Arrays.toString(str1)); // [{name='xiaomi', price=48.0}, {name='oppo', price=298.0}, {name='lenovo', price=18.0}, // {name='huawei', price=98.0}, {name='huawei', price=68.0}, {name='dell', price=98.0}] } ```