<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我思故我強(qiáng)

    List排序(轉(zhuǎn)載)

    ?

    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> - 排序規(guī)則控制器.
    ? */
    ?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> - 方法參數(shù).
    ? */
    ?private Object[] methodeArgs;

    ?/**
    ? * 構(gòu)造函數(shù)(List中存放復(fù)雜對象,并且獲得排序字段值的方法需要參數(shù)).
    ? *
    ? * @param methodName
    ? *??????????? -對象中的方法名
    ? * @param methodeArgs
    ? *??????????? -方法參數(shù)
    ? * @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;
    ?}

    ?/**
    ? * 構(gòu)造函數(shù)(List中存放復(fù)雜對象,并且獲得排序字段值的方法不需要參數(shù)).
    ? *
    ? * @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;

    ?}

    ?/**
    ? * 構(gòu)造函數(shù)(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中存放的是復(fù)雜對象
    ??if (this.methodName != null && !"".equals(this.methodName)) {

    ???// 執(zhí)行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;
    ?}

    ?/**
    ? * 執(zhí)行對象的某個方法.
    ? *
    ? * @param owner
    ? *??????????? -對象
    ? * @param methodName
    ? *??????????? -方法名
    ? * @param methodArgs
    ? *??????????? -方法參數(shù)
    ? *
    ? * @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 李云澤 閱讀(786) 評論(0)  編輯  收藏 所屬分類: J2SE 、Java代碼

    主站蜘蛛池模板: 99精品热线在线观看免费视频| 久久久久久亚洲精品| 国产2021精品视频免费播放| 成年网在线观看免费观看网址 | 2020天堂在线亚洲精品专区| 国产亚洲成AV人片在线观黄桃| 日韩免费视频播放| 无码国产精品一区二区免费式直播 | 中文字幕不卡高清免费| 亚洲精品美女久久7777777| 4480yy私人影院亚洲| 亚洲人成影院在线无码按摩店| 免费播放特黄特色毛片| 成全视频免费高清| 国产a视频精品免费观看| 日本不卡免费新一区二区三区| 无遮挡国产高潮视频免费观看| 亚洲AV成人一区二区三区观看| 亚洲AV无码一区二区三区在线| 久久亚洲私人国产精品| 亚洲国产精品一区二区成人片国内| 亚洲美日韩Av中文字幕无码久久久妻妇| 麻豆国产精品入口免费观看| 在线视频观看免费视频18| 亚洲精品视频免费在线观看| 久久99热精品免费观看牛牛| 国产在线播放线91免费| 国产免费A∨在线播放| 亚洲阿v天堂在线2017免费 | 免费激情视频网站| 曰批全过程免费视频在线观看 | 久久久无码精品亚洲日韩蜜臀浪潮| 黑人精品videos亚洲人| 国产午夜亚洲不卡| 亚洲色爱图小说专区| 亚洲熟妇丰满多毛XXXX| 亚洲真人无码永久在线| 久久久久久久尹人综合网亚洲| 国产精品V亚洲精品V日韩精品 | 亚洲高清一区二区三区电影| 亚洲精品人成网线在线播放va |