<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ù)雜對(duì)象,并且獲得排序字段值的方法需要參數(shù)).
    ? *
    ? * @param methodName
    ? *??????????? -對(duì)象中的方法名
    ? * @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ù)雜對(duì)象,并且獲得排序字段值的方法不需要參數(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中存放簡(jiǎn)單對(duì)象).
    ? *
    ? * @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ù)雜對(duì)象
    ??if (this.methodName != null && !"".equals(this.methodName)) {

    ???// 執(zhí)行Bean中的方法,得到方法返回的對(duì)象
    ???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í)行對(duì)象的某個(gè)方法.
    ? *
    ? * @param owner
    ? *??????????? -對(duì)象
    ? * @param methodName
    ? *??????????? -方法名
    ? * @param methodArgs
    ? *??????????? -方法參數(shù)
    ? *
    ? * @return 方法返回對(duì)象
    ? * @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) 評(píng)論(0)  編輯  收藏 所屬分類: J2SE 、Java代碼

    主站蜘蛛池模板: 黄色一级毛片免费| 久久久久国产精品免费看| 亚洲片一区二区三区| 中文字幕在线免费视频| 亚洲综合区图片小说区| 国产性生交xxxxx免费| 少妇性饥渴无码A区免费 | 日韩内射激情视频在线播放免费| 最近免费中文字幕mv电影| 亚洲欧洲日产国产最新| 免费一级黄色毛片| 在线观看永久免费| 免费无码午夜福利片| 亚洲日韩国产精品无码av| 亚洲五月午夜免费在线视频| 真人做A免费观看| 中文字幕免费观看全部电影| 亚洲人成网站在线播放2019| 亚洲日韩激情无码一区| 国外成人免费高清激情视频| 在线观看免费无码视频| 精品国产亚洲AV麻豆| 亚洲资源在线视频| 精品国产亚洲男女在线线电影 | 亚洲AV综合色区无码一二三区| 91精品视频在线免费观看| 亚洲色偷偷偷综合网| 亚洲国产女人aaa毛片在线| 可以免费观看的一级毛片| 青娱乐免费在线视频| 日批视频网址免费观看| 男女超爽视频免费播放| 亚洲国产成人久久| 久久亚洲精品国产精品黑人| 亚洲国产精品成人网址天堂 | 91精品手机国产免费| 拍拍拍无挡免费视频网站| 国产精品日本亚洲777| 国产成人精品亚洲2020| 亚洲精品白色在线发布| 亚洲国产精品一区二区第一页 |