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