java 多字段排序

	/**
	 * Sort <code>List</code> by multiple conditions.<br>
	 * It's like SQL 'Order by' clause. Input multiple <code>Comparator</code>
	 * in List as argument.
	 * 
	 * @param <T>
	 * @param list
	 * @param comparatorList
	 * @throws IllegalArgumentException if comparatorList is empty
	 * @author shengyuan.lu 卢声远<michaellufhl@yahoo.com.cn>
	 */
	public static <T> void sort(List<T> list, final List<Comparator<T>> comparatorList) {
        if (comparatorList.isEmpty()) {//Always equals, if no Comparator.
             throw new IllegalArgumentException("comparatorList is empty.");
        }
        Comparator<T> comparator = new Comparator<T>() {
        public int compare(T o1, T o2) {
                for (Comparator<T> c:comparatorList) {
                    if (c.compare(o1, o2) > 0) {
                      return 1;
                    } else if (c.compare(o1, o2) < 0) {
                      return -1;
                    }
                }
                return 0;
          }
        };
        Collections.sort(list, comparator);
   }

 

 public void sortByMethod(List<E> list, final String method,
            final boolean reverseFlag) {
        Collections.sort(list, new Comparator<Object>() {
            @SuppressWarnings("unchecked")
            public int compare(Object arg1, Object arg2) {
                int result = 0;
                try {
                    Method m1 = ((E) arg1).getClass().getMethod(method, null);
                    Method m2 = ((E) arg2).getClass().getMethod(method, null);
                    Object obj1 = m1.invoke(((E)arg1), null);
                    Object obj2 = m2.invoke(((E)arg2), null);
                    if(obj1 instanceof String) {
                        // 字符串
                        result = obj1.toString().compareTo(obj2.toString());
                    }else if(obj1 instanceof Date) {
                        // 日期
                        long l = ((Date)obj1).getTime() - ((Date)obj2).getTime();
                        if(l > 0) {
                            result = 1;
                        }else if(l < 0) {
                            result = -1;
                        }else {
                            result = 0;
                        }
                    }else if(obj1 instanceof Integer) {
                        // 整型(Method的返回参数可以是int的,因为JDK1.5之后,Integer与int可以自动转换了)
                        result = (Integer)obj1 - (Integer)obj2;
                    }else {
                        // 目前尚不支持的对象,直接转换为String,然后比较,后果未知
                        result = obj1.toString().compareTo(obj2.toString());
                        
                        System.err.println("MySortList.sortByMethod方法接受到不可识别的对象类型,转换为字符串后比较返回...");
                    }
                    
                    if (reverseFlag) {
                        // 倒序
                        result = -result;
                    }
                } catch (NoSuchMethodException nsme) {
                    nsme.printStackTrace();
                } catch (IllegalAccessException iae) {
                    iae.printStackTrace();
                } catch (InvocationTargetException ite) {
                    ite.printStackTrace();
                }

                return result;
            }
        });
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值