java_排序_正序
程序员文章站
2022-03-21 21:13:02
...
java_排序_正序 List
List<Integer> integerList = new ArrayList<>();
integerList.add(6);
integerList.add(3);
integerList.add(2);
integerList.add(5);
class MyComparator implements Comparator<Integer>{
public int compare(Integer i1, Integer i2) {
if (i1 < i2)
return -1;
else if (i1 == i2)
return 0;
else return 1;
}
}
Comparator<Integer> px = new MyComparator();
Collections.sort(integerList,px);
System.out.println("第一位___"+integerList.get(0));
排序List<实体>
//实体对象
public class DoubleDto {
private Integer villageId;
//距离
private Double distance;
}
//排序:
class MyComparator implements Comparator<DoubleDto>{
public int compare(DoubleDto i1, DoubleDto i2) {
if (i1.getDistance() < i2.getDistance())
return -1;
else if (i1 == i2)
return 0;
else return 1;
}
}
Comparator<DoubleDto> px = new MyComparator();
Collections.sort(doubleDtos,px);
//获取最小的一个
System.out.println(doubleDtos.get(0));
上一篇: vue-cli3构建ts项目