我思故我強

          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 李云澤 閱讀(789) 評論(0)  編輯  收藏 所屬分類: J2SEJava代碼

          主站蜘蛛池模板: 连州市| 北海市| 丰顺县| 囊谦县| 红河县| 集贤县| 措美县| 偏关县| 荥阳市| 云阳县| 白玉县| 宜兰市| 长子县| 曲阜市| 丁青县| 大方县| 西城区| 佛教| 湾仔区| 新巴尔虎左旗| 郧西县| 金阳县| 原阳县| 黑龙江省| 太谷县| 宁武县| 旬邑县| 咸宁市| 运城市| 道孚县| 天长市| 哈密市| 横峰县| 城市| 南城县| 同德县| 绥滨县| 珠海市| 炉霍县| 隆子县| 白水县|