Collections.sort(list, new Comparator() {
/*
* 返回一个基本类型的整型, 返回负数表示:o1 小于o2, 返回0 表示:o1和o2相等, 返回正数表示:o1大于o2。
*/
@Override
public int compare(ResourcesDto o1, ResourcesDto o2) {
if (o1.getDistance() > o2.getDistance()) {
return 1;
}
if (o1.getDistance() == o2.getDistance()) {
return 0;
}
return -1;
}
});
源码中是这样写的,在网上搜索很多都没有解决,有不能用1.6。
getDistance()返回什么类型?float或者double么?浮点数要考虑误差,不能用==比较,应该这么写
if (Math.abs(o1.getDistance() - o2.getDistance()) < 0.000001)
...