我思故我强

List排序(转载)

 

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.CollationKey;
import java.text.Collator;
import java.text.RuleBasedCollator;
import java.util.*;


/**
 * <strong>Title : ListComparator </strong>. <br>
 * <strong>Description : List排序器(支持中文排序).</strong> <br>
  */
public class ListComparator implements Comparator {


 /**
  * <code>collator</code> - 排序规则控制器.
  */
 private RuleBasedCollator collator = (RuleBasedCollator) Collator
   .getInstance(java.util.Locale.CHINA);

 /**
  * <code>methodName</code> - 排序字段的方法名.
  */
 private String methodName;

 /**
  * <code>seq</code> - 排序顺序.
  */
 private String seq;

 /**
  * <code>methodeArgs</code> - 方法参数.
  */
 private Object[] methodeArgs;

 /**
  * 构造函数(List中存放复杂对象,并且获得排序字段值的方法需要参数).
  *
  * @param methodName
  *            -对象中的方法名
  * @param methodeArgs
  *            -方法参数
  * @param seq
  *            -排序顺序
  * @throws Exception
  */
 public ListComparator(String methodName, Object[] methodeArgs, String seq)
   throws Exception {

  this.methodName = methodName;

  this.methodeArgs = methodeArgs;

  if (!"asc".equalsIgnoreCase(seq) && !"desc".equalsIgnoreCase(seq)) {

   throw new Exception("illegal value of parameter 'seq' for input '"
     + seq + "'");
  }

  this.seq = seq;
 }

 /**
  * 构造函数(List中存放复杂对象,并且获得排序字段值的方法不需要参数).
  *
  * @param methodName
  * @param seq
  * @throws Exception
  */
 public ListComparator(String methodName, String seq) throws Exception {

  this.methodName = methodName;

  if (!"asc".equalsIgnoreCase(seq) && !"desc".equalsIgnoreCase(seq)) {

   throw new Exception("illegal value of parameter 'seq' for input '"
     + seq + "'");
  }

  this.seq = seq;

 }

 /**
  * 构造函数(List中存放简单对象).
  *
  * @param seq
  * @throws Exception
  */
 public ListComparator(String seq) throws Exception {

  if (!"asc".equalsIgnoreCase(seq) && !"desc".equalsIgnoreCase(seq)) {

   throw new Exception("illegal value of parameter 'seq' for input '"
     + seq + "'");
  }

  this.seq = seq;
 }

 /**
  * (non-Javadoc).
  *
  * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
  */
 public int compare(Object obj1, Object obj2) {

  int t_ret = 0;

  // 如果指定了方法名,则表示List中存放的是复杂对象
  if (this.methodName != null && !"".equals(this.methodName)) {

   // 执行Bean中的方法,得到方法返回的对象
   Object t_obj1 = invokeMethod(obj1, this.methodName,
     this.methodeArgs);
   Object t_obj2 = invokeMethod(obj2, this.methodName,
     this.methodeArgs);

   t_ret = selectCompare(t_obj1, t_obj2);

  } else {

   t_ret = selectCompare(obj1, obj2);

  }

  return t_ret;
 }

 /**
  * 执行对象的某个方法.
  *
  * @param owner
  *            -对象
  * @param methodName
  *            -方法名
  * @param methodArgs
  *            -方法参数
  *
  * @return 方法返回对象
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  * @throws IllegalArgumentException
  * @throws Exception
  */
 private Object invokeMethod(Object owner, String methodName,
   Object[] methodArgs) {

  Class[] argsClass = null;

  if (methodArgs != null && methodArgs.length > 0) {

   argsClass = new Class[methodeArgs.length];

   for (int i = 0, j = methodeArgs.length; i < j; i++) {

    argsClass[i] = methodeArgs[i].getClass();
   }
  }

  Class ownerClass = owner.getClass();

  Method method;
  Object t_object = null;
  try {
   method = ownerClass.getMethod(methodName, argsClass);
   t_object = method.invoke(owner, methodArgs);
  } catch (SecurityException e) {
  } catch (NoSuchMethodException e) {

   argsClass = new Class[1];
   argsClass[0] = Object.class;
   try {
    method = ownerClass.getMethod(methodName, argsClass);
    t_object = method.invoke(owner, methodArgs);
   } catch (SecurityException e1) {
   } catch (NoSuchMethodException e1) {
   } catch (IllegalArgumentException e1) {
   } catch (IllegalAccessException e1) {
   } catch (InvocationTargetException e1) {
   }

  } catch (IllegalArgumentException e) {
  } catch (IllegalAccessException e) {
  } catch (InvocationTargetException e) {
  }

  return t_object;
 }


 private int selectCompare(Object obj1, Object obj2) {

  int t_ret = 0;

  if (obj1 instanceof String && obj2 instanceof String) {

   t_ret = compareString(obj1, obj2);
  }

  if (obj1 instanceof Integer && obj2 instanceof Integer) {

   t_ret = compareInt(obj1, obj2);
  }

  if (obj1 instanceof Long && obj2 instanceof Long) {

   t_ret = compareLong(obj1, obj2);
  }

  if (obj1 instanceof Date && obj2 instanceof Date) {

   t_ret = compareDate(obj1, obj2);
  }

  return t_ret;
 }

 private int compareString(Object obj1, Object obj2) {

  int ret = 0;

  String s1 = (String) obj1;
  String s2 = (String) obj2;

  CollationKey c1 = collator.getCollationKey(s1);
  CollationKey c2 = collator.getCollationKey(s2);

  ret = collator.compare(c1.getSourceString(), c2.getSourceString());

  if (seq.equalsIgnoreCase("desc")) {

   ret = ret * -1;
  }

  return ret;
 }

 private int compareInt(Object obj1, Object obj2) {

  int ret = 0;

  int i1 = ((Integer) obj1).intValue();
  int i2 = ((Integer) obj2).intValue();

  if (i1 < i2)
   ret = -1;
  else if (i1 > i2)
   ret = 1;
  else
   ret = 0;

  if (seq.equalsIgnoreCase("desc")) {

   ret = ret * -1;
  }

  return ret;
 }

 private int compareLong(Object obj1, Object obj2) {

  int ret = 0;

  long l1 = ((Long) obj1).longValue();
  long l2 = ((Long) obj2).longValue();

  if (l1 < l2)
   ret = -1;
  else if (l1 > l2)
   ret = 1;
  else
   ret = 0;

  if (seq.equalsIgnoreCase("desc")) {

   ret = ret * -1;
  }

  return ret;
 }

 private int compareDate(Object obj1, Object obj2) {

  int ret = 0;

  Date d1 = (Date) obj1;
  Date d2 = (Date) obj2;

  ret = d1.compareTo(d2);

  if (seq.equalsIgnoreCase("desc")) {

   ret = ret * -1;
  }

  return ret;
 }
}



-------------------------------------------------------------------
使用
-------------------------------------------------------------------
private List getSortedList(List unsortedList, String methodName,
   Object methodArgs[], String orderSequence) throws Exception {

  ListComparator t_compare = null;

  if (null != methodName && !"".equals(methodName)) {

   if (methodArgs != null && methodArgs.length > 0)
    t_compare = new ListComparator(methodName, methodArgs,
      orderSequence);
   else
    t_compare = new ListComparator(methodName, orderSequence);
  } else {
   t_compare = new ListComparator(orderSequence);
  }

  List t_list = unsortedList;

  Collections.sort(t_list, t_compare);

  return t_list;
 }



posted on 2009-04-08 11:33 李云泽 阅读(777) 评论(0)  编辑  收藏 所属分类: J2SEJava代码


只有注册用户登录后才能发表评论。


网站导航: