android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)
2012-05-04 10:31 java20130722 阅读(510) 评论(0) 编辑 收藏 举报Info.java :
public class Info implements Cloneable, Comparable<Info>{ private int id ; public Info(int id ){ this.id = id ; } public int getId() { return id; } @Override public Object clone() throws CloneNotSupportedException { Info info = (Info) super.clone(); return info; } @Override public int compareTo(Info o) { if(this.id > o.id ){ return 1 ; } return 0; } }
在JAVA中 Collections.sort(List<T> list) 的测试:
List<Info> list = new ArrayList<Info>() ; list.add( new Info(3)) ; list.add( new Info(2)) ; list.add( new Info(1)) ; list.add( new Info(4)) ; list.add( new Info(5)) ; Collections.sort(list) ; for(int i = 0 ; i < 5 ; i ++){ System.out.println(list.get(i).getId()); }
结果:
1
2
3
4
5
在android 中 Collections.sort(List<T> list) 的测试:
List<Info> list = new ArrayList<Info>() ; list.add( new Info(3)) ; list.add( new Info(2)) ; list.add( new Info(1)) ; list.add( new Info(4)) ; list.add( new Info(5)) ; Collections.sort(list) ; for(int i = 0 ; i < 5 ; i ++){ System.out.println(list.get(i).getId()); }
结果:
3
2
1
4
5
在android中实现如果排序的方法可以用Collections.sort(List<T> list, new Comparator<Info>() {
@Override public int compare(Info lhs, Info rhs) { return rhs.getId() - lhs.getId(); } }
可以实现对list的排序。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步