List<对象> 排序问题
开发中很多时候由于各种原因,导致最后的list没有顺序,二页面上恰巧需要排序显示,这时候就需要用到 Comparable 这个接口 的 compareTo 这个方法了
首先 我们需要实现 这个接口
public class 类名 implements Comparable<类名>
然后重写他的方法
public int compareTo(类名 o) {
return o.rowCount-this.rowCount;
}
这样 返回的集合就能使用排序了
Collections.sort(集合);
这样 一个降序的就好了
以下是全部实体
public class 类名 implements Comparable<类名>{
private Integer rowCount;
public Integer getRowCount() {
return rowCount;
}
public void setRowCount(Integer rowCount) {
this.rowCount = rowCount;
}
public int compareTo(类名 o) {
return o.rowCount-this.rowCount; 降序
return this.rowCount-o.rowCount; 升序
}
}
如此就不需要写什么工具类了 其他排序请参考Collections的 自测