升序或降序:
升序:Collections.sort(arr);
降序:Collections.sort(arr,new Comparator(){
public int compare(Integer o1,Integer o2){
return o2-o1;
}
});
重写sort方法:
Collections.sort(arr,new Comparator(){
public int compare(Object o1,Object o2){
point s1=(point) o1;
point s2=(point) o2;
if(s1.x>s2.x){
return 1;
}
if(s1.x
return -1;
}
return 0;
}
});//按照x排序
举例:class point{
public double x;
public double y;
};
public class zuobiao{
public static void main(String[] args){
ArrayList arr=new ArrayList();
point p1=new point();
p1.x=14;
p1.y=15;
arr.add(p1);
point p2=new point();
p2.x=15;
p2.y=14;
arr.add(p2);
Collections.sort(arr,new Comparator(){
public int compare(Object o1,Object o2){
point s1=(point) o1;
point s2=(point) o2;
if(s1.x>s2.x){
return 1;
}
if(s1.x
return -1;
}
return 0;
}
});
for(int i=0;i
System.out.println(arr.get(i).x+” “+arr.get(i).y);
}
}
}