java通用对象集合指定属性排序工具类

利用java反射机制,对列表指定属性进行排序,通用工具类

package ygw.study.tool;

import java.lang.reflect.Method;
import java.util.*;

/**
 * <p>
 * 对象列表指定属性进行排序
 * </p>
 * <p>
 * Copyright:.All rights reserved.
 * </p>
 * <p>
 * Company:ygw
 * </p>
 * <p>
 * CreateDate: 2023/2/3
 * </p>
 * 
 * @author YuGongWen
 * @history Mender: YuGongWen;Date: 2023/2/3;
 */
public class ListSortUtil {

    /**
     * @param targetList 目标排序List
     * @param field 排序字段(实体类属性名)
     * @param direction 排序方式(asc or desc),默认升序
     * @param absSort 是否取绝对值
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static<T> void sort(List<T> targetList, final String field, final String direction, final boolean absSort) {

        if(targetList==null || field==null)
            return;
        Collections.sort(targetList, new Comparator() {
            public int compare(Object obj1, Object obj2) {
                int retVal = 0;
                try {
                    // 首字母转大写
                    String newStr = field.substring(0, 1).toUpperCase() + field.replaceFirst("\\w", "");
                    String methodStr = "get" + newStr;
                    Method method1 = ((T) obj1).getClass().getMethod(methodStr, new Class[]{});
                    Method method2 = ((T) obj2).getClass().getMethod(methodStr, new Class[]{});
                    Object value2 = method2.invoke(((T) obj2), new Object[]{});
                    Object value = method1.invoke(((T) obj1), new Object[]{});
                    if(value!=null && value2!=null){
                        if(value instanceof Byte){
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = ((Byte)value2).compareTo((Byte)value);
                            } else {//正序
                                retVal = ((Byte)value).compareTo((Byte)value2);
                            }
                        }else if(value instanceof Double){
                            Double v1 = (Double) value;
                            Double v2 = (Double) value2;
                            if(absSort){
                                v1 = Math.abs(v1);
                                v2 = Math.abs(v2);
                            }
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = v2.compareTo(v1);
                            } else {//正序
                                retVal = v1.compareTo(v2);
                            }
                        }else if(value instanceof Float){
                            Float v1 = (Float) value;
                            Float v2 = (Float) value2;
                            if(absSort){
                                v1 = Math.abs(v1);
                                v2 = Math.abs(v2);
                            }
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = v2.compareTo(v1);
                            } else {//正序
                                retVal = v1.compareTo(v2);
                            }
                        }else if(value instanceof Integer){
                            Integer v1 = (Integer) value;
                            Integer v2 = (Integer) value2;
                            if(absSort){
                                v1 = Math.abs(v1);
                                v2 = Math.abs(v2);
                            }
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = v2.compareTo(v1);
                            } else {//正序
                                retVal = v1.compareTo(v2);
                            }
                        }else if(value instanceof Long){
                            Long v1 = (Long) value;
                            Long v2 = (Long) value2;
                            if(absSort){
                                v1 = Math.abs(v1);
                                v2 = Math.abs(v2);
                            }
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = v2.compareTo(v1);
                            } else {//正序
                                retVal = v1.compareTo(v2);
                            }
                        }else if(value instanceof Short){
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = ((Short)value2).compareTo((Short)value);
                            } else {//正序
                                retVal = ((Short)value).compareTo((Short)value2);
                            }
                        }else if(value instanceof Date){
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = ((Date)value2).compareTo((Date)value);
                            } else {//正序
                                retVal = ((Date)value).compareTo((Date)value2);
                            }
                        }else{
                            if (direction != null && "desc".equals(direction)) {// 倒序
                                retVal = ((String)value2).compareTo((String)value);
                            } else {//正序
                                retVal = ((String)value).compareTo((String)value2);
                            }
                        }
                    }
                } catch (Exception e) {
                    //e.printStackTrace();
                }
                return retVal;
            }
        });
    }

    public static void main(String[] args) {
        // 所有记录按更新时间倒序排序
        List<BizRecord> recordList = new ArrayList<>();
        ListSortUtil.sort(recordList, "lastModifiedDate", "desc", false);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值