android Collections.sort(List<T> list) 与JAVA Collections.sort(List<T> list)

Info.java :

[java]  view plain copy
  1. public class Info implements Cloneable, Comparable<Info>{  
  2. private int id ;  
  3. public Info(int id ){  
  4. this.id = id ;  
  5. }  
  6.   
  7. public int getId() {  
  8. return id;  
  9. }  
  10.   
  11. @Override  
  12.    public Object clone() throws CloneNotSupportedException {  
  13.   Info info = (Info) super.clone();  
  14.   return info;  
  15.    }  
  16.   
  17. @Override  
  18. public int compareTo(Info o) {  
  19. if(this.id > o.id ){  
  20. return 1 ;  
  21. }  
  22. return 0;  
  23. }  
  24. }  





在JAVA中 Collections.sort(List<T> list) 的测试:


[java]  view plain copy
  1. List<Info> list = new ArrayList<Info>() ;  
  2. list.add( new Info(3)) ;  
  3. list.add( new Info(2)) ;  
  4. list.add( new Info(1)) ;  
  5. list.add( new Info(4)) ;  
  6. list.add( new Info(5)) ;  
  7. Collections.sort(list) ;  
  8. for(int i = 0 ; i < 5 ; i ++){  
  9. System.out.println(list.get(i).getId());  
  10. }  


结果:

1

2

3

4

5


在android 中 Collections.sort(List<T> list) 的测试:

[java]  view plain copy
  1. List<Info> list = new ArrayList<Info>() ;  
  2. list.add( new Info(3)) ;  
  3. list.add( new Info(2)) ;  
  4. list.add( new Info(1)) ;  
  5. list.add( new Info(4)) ;  
  6. list.add( new Info(5)) ;  
  7. Collections.sort(list) ;  
  8. for(int i = 0 ; i < 5 ; i ++){  
  9. System.out.println(list.get(i).getId());  
  10. }  


结果:

3

2

1

4

5

在android中实现如果排序的方法可以用Collections.sort(List<T> list, new Comparator<Info>() {

[java]  view plain copy
  1. @Override  
  2. public int compare(Info lhs, Info rhs) {  
  3. return rhs.getId() - lhs.getId();  
  4. }  
  5.   }  

可以实现对list的排序。


转载:http://blog.csdn.net/luhuajcdd/article/details/7533956

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值