List<Point>listp=new ArrayList<>();
Point a=new Point("a",1.0);
Point b=new Point("b",3.0);
Point c=new Point("c",2.0);
Point d=new Point("d",4.0);
listp.add(a);
listp.add(b);
listp.add(c);
listp.add(d);
Collections.sort(listp);
for(int k=0;k<listp.size();k++){
System.out.println(listp.get(k).getKey());
}
public class Point implements Comparable {
public Point(String key, double r) {
this.key = key;
this.r = r;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
private String key;
private double r;
@Override
public int compareTo(Object arg0) {
Point a1=this;
Point a2=(Point)arg0;
if(a1.getR()>a2.getR()){
return -1;
}
else if(a1.getR()<a2.getR()){
return 1;
}
else
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步