java降序排列


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;
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM