DESKTOP-I6FA4UK_20200901-刘光超
1、问题
// 使用 Date 类提供的方法,对日期进行比较并排序 (自然排序) 是直接 Arrays.sort(dates);吗。。。
Arrays.sort(dates);
// 使用 Date 类提供的方法,对日期进行比较并排序 (比较器排序)
//解决:
//新建一个类Select 继承 Comparator 重写 compare 在
public class Select implements Comparator<Date>{
@Override
public int compare(Date o1, Date o2) {
if(o1.equals(o2)) {
return 0;
}else if(o1.after(o2)) {
return 1;
}else {
return -1;
}
}
Comparator<Date> a = new Select();
// Arrays.sort(dates, a );
for (int x = 0, n = dates.length - 1; x < n; x++) {
for (int y = 0; y < n - x; y++) {
if (a.compare(dates[y], dates[y + 1]) > 0) {
Date temp = dates[y + 1];
dates[y + 1] = dates[y];
dates[y] = temp;
}
}
}
2、吐槽
无力吐槽。。。
近期评论