1.是list中的对象实现Comparable接口:
/**
* 根据order对User排序
*/
public class User implements Comparable<user>{
private String name;
private Integer order;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public int compareTo(User arg0) {
return this.getOrder().compareTo(arg0.getOrder());
}
}
2.根据Collections.sort重载方法来实现:
Collections.sort(list,new Comparator<user>(){
public int compare(User arg0, User arg1) {
return arg0.getOrder().compareTo(arg1.getOrder());
}
});
3.使用compareTo的注意事项
这个是提供给Integer,int则不可以。