java List Collections.sort 排序 支持 对象属性 普通数据类型

  升序 降序 正序 倒序 排列

Java代码   收藏代码
  1. import java.lang.reflect.Method;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.*;  
  4.   
  5. public class ListComparator implements Comparator {  
  6.   
  7.     public ListComparator() {}  
  8.   
  9.     public ListComparator(String methodName) {  
  10.         this.methodName = methodName;  
  11.     }  
  12.   
  13.     //空:普通的数据类型 非空:对象数据类型 取methodName返回值比较  
  14.     String methodName;  
  15.       
  16.     public Object getValue(Object bean, String methodName) throws Exception {  
  17.         Method getMethod = bean.getClass().getMethod(methodName, null);  
  18.         return getMethod.invoke(bean, null);  
  19.     }  
  20.   
  21.     public int compare(Object obj1, Object obj2) {  
  22.         int val = -1;  
  23.         try {  
  24.             if ( methodName == null ) {  
  25.                 val = _compare(obj1, obj2);  
  26.             } else {  
  27.                 val = _compare(getValue(obj1, methodName), getValue(obj2, methodName));  
  28.             }  
  29.         } catch (Exception ex) {ex.printStackTrace();}  
  30.         return val;  
  31.     }  
  32.   
  33.     public int _compare(Object obj1, Object obj2) throws Exception {  
  34.         if ( obj1 == null || obj2 == null ) return obj1 == null ? -1 : 1;  
  35.         Class cl = obj1.getClass();  
  36.         if ( obj1 instanceof java.lang.Comparable ) {  
  37.         //byte int long float..number, date , boolean , char  
  38.             Method getMethod = obj1.getClass().getMethod("compareTo"new Class[]{cl});  
  39.             return (Integer) getMethod.invoke(obj1, new Object[]{obj2});  
  40.         }  
  41.         return -1;  
  42.     }  
  43.   
  44. }  
  45.   
  46. //测试类  
  47. class Test {  
  48.     Long id;  
  49.     public Long getId() {  
  50.         return id;  
  51.     }  
  52.     public void setId(Long id) {  
  53.         this.id = id;  
  54.     }  
  55.   
  56.     public static void main(String[] args) {  
  57.   
  58.         //普通数据类型  升序  
  59.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");  
  60.         List list = new ArrayList();  
  61.         for (long i = 0; i < 5; i++) {  
  62.             list.add( new Date(System.currentTimeMillis() + (int) (Math.random() * 1000000000)) );  
  63.         }  
  64.   
  65.         ListComparator c = new ListComparator();  
  66.         Collections.sort(list, c);  
  67.   
  68.         for (int i = 0; i < list.size(); i++) {  
  69.             System.out.println(simpleDateFormat.format(list.get(i)));  
  70.         }  
  71.   
  72.          //对象数据类型 降序  
  73.         List<Test> userList = new ArrayList();  
  74.         Test test = new Test();  
  75.         test.setId(null);  
  76.         userList.add(test);  
  77.   
  78.         for (long i = 0; i < 5; i++) {  
  79.             test = new Test();  
  80.             test.setId(i);  
  81.             userList.add(test);  
  82.         }  
  83.   
  84.         c = new ListComparator("getId");  
  85.         Collections.sort(userList,Collections.reverseOrder(c));  
  86.           
  87.         for (int i = 0; i < userList.size(); i++) {  
  88.             System.out.println(userList.get(i).getId());  
  89.         }  
  90.   
  91.     }  
  92.   


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值