在JAVA中针对LIST进行排序

工作原因,需要针对实时爬取的数据进行排序,而这些数据没有存在于DB中。所以,就要在JAVA中进行排序。

为此,写了一个基于Collections.sort来排序的方法。

说明:

1 当数据没有存在于DB中采用此种排序。

2 本排序只提供单列,不提供复合,有需要的可以自行重写。

3 本排序基于Collections.sort,重写Comparator()来实现。

4 排序针对于LIST,参数为:LIST,字段名,字段类型,升/降序。

5 排序支持类型为 int,double,string,date


主方法: SortListUitl.java


package com.xbniao.analysis.compare.common.util.sort;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

public class SortListUtil<E> {
    public static final String FIELD_TYPE_INTEGER = "Integer";
    public static final String FIELD_TYPE_STRING = "String";
    public static final String FIELD_TYPE_DOUBLE = "Double";
    public static final String FIELD_TYPE_DATE = "Date";
    public static final String FIELD_SORT_ASC = "asc";
    public static final String FIELD_SORT_DESC = "desc";
    /**
     * Sort 本排序只支持单列排序,不支持复合排序
     * @param list 排序list
     * @param field 排序字段
     * @param fieldType 字段类型
     * @param sort 升、降序
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void sort(List<E> list, String field, final String fieldType, final String sort) {
        if (CollectionUtils.isEmpty(list) || StringUtils.isBlank(field))  {
            return;
        }
        String firstChar = field.substring(0,1).toUpperCase();
        field = "get" + firstChar + field.substring(1);
        final String method = field;
        Collections.sort(list, new Comparator() {
            public int compare(Object o1, Object o2) {
                int result = 0;
                if (null != o1 && null != o2) {
                    try {
                        Object obj1 = ReflectUtils.getObjectValue(o1, method);
                        Object obj2 = ReflectUtils.getObjectValue(o2, method);
                        if (FIELD_TYPE_INTEGER.equals(fieldType)) {
                            // Integer型比较
                            int num1;
                            int num2;
                            if (obj1 instanceof Integer) {
                                num1 = (Integer) obj1;
                                num2 = (Integer) obj2;
                            } else {
                                num1 = 0;
                                num2 = 0;
                                if ((obj1 instanceof String) && StringUtils.isNotEmpty(String.valueOf(obj1))) { 
                                    num1 = Integer.parseInt(obj1.toString());
                                }
                                if ((obj2 instanceof String) && StringUtils.isNotEmpty(String.valueOf(obj2))) { 
                                    num2 = Integer.parseInt(obj2.toString());
                                }
                            }
                            if (obj1 == null || obj2 == null) {
                                if (obj1 == null) result = -1;
                                if (obj2 == null) result = 1;
                            } else {
                                if (num1 > num2) {
                                    result = 1;
                                } else if (num1 < num2) {
                                    result = -1;
                                } else {
                                    result = 0;
                                }
                            }
                        } else if (FIELD_TYPE_DOUBLE.equals(fieldType)) {
                            // DOUBLE 型比较
                            BigDecimal num1;
                            BigDecimal num2;
                            if (obj1 instanceof Double) {
                                num1 = new BigDecimal((Double) obj1);
                                num2 = new BigDecimal((Double) obj2);
                            } else {
                                num1 = new BigDecimal(0);
                                num2 = new BigDecimal(0);
                                if ((obj1 instanceof String) && StringUtils.isNotEmpty(String.valueOf(obj1))) {
                                    num1 = new BigDecimal(Double.parseDouble(obj1.toString()));
                                }
                                if ((obj2 instanceof String) && StringUtils.isNotEmpty(String.valueOf(obj2))) {
                                    num2 = new BigDecimal(Double.parseDouble(obj2.toString()));
                                }
                            }
                            if (obj1 == null || obj2 == null) {
                                if (obj1 == null) result = -1;
                                if (obj2 == null) result = 1;
                            } else {
                                result = num1.compareTo(num2);
                            }
                        } else if (FIELD_TYPE_DATE.equals(fieldType)) {
                            // Date 型比较
                            DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                            Date dt1 = df.parse(String.valueOf(obj1));
                            Date dt2 = df.parse(String.valueOf(obj2));
                            if (dt1.getTime() > dt2.getTime()) {
                                result = 1;
                            } else if (dt1.getTime() < dt2.getTime()) {
                                result = -1;
                            } else {
                                result = 0;
                            }
                        } else {
                            // String 型比较
                            if (obj1 == null || obj2 == null) {
                                if (obj1 == null) result = -1;
                                if (obj2 == null) result = 1;
                            } else {
                                result = obj1.toString().compareTo(obj2.toString());
                            }
                        }
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                // 排序设置
                if (FIELD_SORT_DESC.equals(sort)) {
                    if (result < 0) {
                        return 1;
                    } else if (result > 0) {
                        return -1;
                    }
                }
                return result/* 等于 */;
            }
        });
    }
}



反射方法:ReflectUtils.java
package com.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectUtils {

    public static Object getObjectValue(Object o1, String method) {
        try {
            Method method = o1.getClass().getMethod(method, null);
            return method.invoke(o1);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return new Object();
    }
}

操作:


        SortListUtil <testList> sortList = new SortListUtil<testList>();
        sortList.sort(abcList, "name",SortListUtil.FIELD_TYPE_STRING , SortListUtil.FIELD_SORT_ASC);

本文参考了

http://www.cnblogs.com/huangwei520/p/4868934.html

对此表示感谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值