<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代碼

    主站蜘蛛池模板: 182tv免费视频在线观看| 无限动漫网在线观看免费| 99久久成人国产精品免费| 成人午夜免费福利视频| 免费一级毛片在线观看| 久久精品国产亚洲AV电影| 香蕉97碰碰视频免费| 1000部免费啪啪十八未年禁止观看 | 国产精品成人免费视频网站京东| 免费一级毛片不卡在线播放 | 一边摸一边爽一边叫床免费视频| 最近中文字幕完整免费视频ww | 最近2022中文字幕免费视频| 亚洲精品在线免费观看视频| 亚洲免费在线观看| 免费看美女让人桶尿口| 内射少妇36P亚洲区| 国产一卡2卡3卡4卡2021免费观看| 亚洲理论电影在线观看| 美女免费精品高清毛片在线视| 免费观看四虎精品国产永久| 亚洲熟妇少妇任你躁在线观看| 国产白丝无码免费视频| www亚洲精品少妇裸乳一区二区| 7777久久亚洲中文字幕| 69影院毛片免费观看视频在线| 亚洲另类图片另类电影| 日韩电影免费观看| 亚洲国产精品综合久久2007| 一级毛片免费播放| 亚洲成AV人在线播放无码| 在线观看免费黄网站| 亚洲国产中文在线视频| 国产福利免费在线观看| AV激情亚洲男人的天堂国语| 国产精品高清全国免费观看| a级在线观看免费| 亚洲国产综合精品中文第一| 91视频国产免费| 一个人免费观看视频在线中文 | 又黄又大又爽免费视频|