Java Compare
2021-04-20 09:28
标签:static ide ble tor name rgs -- http attention Java Compare 标签:static ide ble tor name rgs -- http attention 原文地址:https://www.cnblogs.com/nedrain/p/13285391.html
Comparable
// Goods.java
import java.util.Arrays;
public class Goods implements Comparable {
private String name;
private double price;
public Goods() {
}
public Goods(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"name=‘" + name + ‘\‘‘ +
", price=" + price +
‘}‘;
}
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods = (Goods) o;
if(this.price
Comparator
ATTENTION : Comparator 一般用于在Arrays.sort() 或 Collections.sort()中以匿名实现类的方式使用
@Test
public void test1(){
String[] arrs = new String[]{"XZZ", "BB", "AA", "CC", "JJ", "DD", "EE"};
Arrays.sort(arrs, new Comparator