对list<obj> 的object任意字段进行排序,默认按照字段属性排序,适用于不分页的返回体

本文介绍了一款用于根据前端传来的VO字段类型进行排序的工具,适用于字符串、BigDecimal、日期和整数类型的字段,支持升序和降序排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 依照前端传过来的vo字段 获取字段类型 进行排序
 * @author fuzhi
 * @date 2019-01-15
 */

@Slf4j
public class SortUtil {

    /**
     * @param list 返回给前端的list
     * @param orderBy 要排序字段
     * @param direction 正序 asc 倒序 desc
     * @return List
     */
    public static List getSortedList(List list, String orderBy, String direction) {
        if (StringUtils.isNotBlank(orderBy) && StringUtils.isNotBlank(direction)) {
            List li = (List) list.stream().sorted((o1, o2) -> {
                try {
                    Field field1 = o1.getClass().getDeclaredField(orderBy);
                    Field field2 = o2.getClass().getDeclaredField(orderBy);
                    /** 获取private属性*/
                    field1.setAccessible(true);
                    field2.setAccessible(true);

                 if(field1.getType().toString().endsWith("String")){
                        String a1 = (String) field1.get(o1);
                        String a2 = (String) field2.get(o2);
                     if ((null != a1) && (null != a2)) {
                         String a11 = new String(a1.getBytes("GB2312"),"ISO-8859-1");
                         String a22 = new String(a2.getBytes("GB2312"),"ISO-8859-1");
                        if ("asc".equals(direction)) {
                            return a11.compareTo(a22);
                        }
                        if ("desc".equals(direction)) {
                            return a22.compareTo(a11);
                        }
                    }else {
                         {
                             if ("asc".equals(direction)) { if (null == a1) {return -1;}return 1; }
                             if ("desc".equals(direction)) { if (null == a1) {return 1;}else { return -1; }}
                         }
                     }
                 }

                    if (field1.getType().toString().endsWith("BigDecimal")) {
                        BigDecimal a1 = ((BigDecimal) field1.get(o1));
                        BigDecimal a2 = (BigDecimal) field2.get(o2);

                        if ((null != a1) && (null != a2)) {
                            if ("asc".equals(direction)) {
                                return a1.compareTo(a2);
                            }
                            if ("desc".equals(direction)) {
                                return a2.compareTo(a1);
                            }
                        } else {
                            {
                                if ("asc".equals(direction)) { if (null == a1) {return -1;}return 1; }
                                if ("desc".equals(direction)) { if (null == a1) {return 1;}else { return -1; }}
                            }
                        }
                    }

                    if (field1.getType().toString().endsWith("Date")) {
                        Date a1 = ((Date) field1.get(o1));
                        Date a2 = (Date) field2.get(o2);
                        if ((null != a1) && (null != a2)) {
                            if ("asc".equals(direction)) {
                                return a1.compareTo(a2);
                            }
                            if ("desc".equals(direction)) {
                                return a2.compareTo(a1);
                            }
                        } else {
                            {
                                if ("asc".equals(direction)) {
                                    if (null == a1) { return -1; }else { return 1; }}
                                if ("desc".equals(direction)) {
                                    if (null == a1) {return 1;}else{ return -1; }}
                            }
                        }
                    }

                    if(field1.getType().toString().endsWith("Int")||field1.getType().toString().endsWith("Integer")){
                        Integer a1 = ((Integer) field1.get(o1));
                        Integer a2 = (Integer) field2.get(o2);
                        if ((null != a1) && (null != a2)) {
                            if ("asc".equals(direction)) {
                                return a1.compareTo(a2);
                            }
                            if ("desc".equals(direction)) {
                                return a2.compareTo(a1);
                            }
                        } else {
                            {
                                if ("asc".equals(direction)) {
                                    if (null == a1) { return -1;}else { return 1;} }
                                if ("desc".equals(direction)) {
                                    if (null == a1) {return 1;} else{ return -1; } }
                            }
                        }
                    }
                } catch (Exception e) {
                    log.info("自定义字段排序失败" + e.getMessage());
                }
                return 1;
            }).collect(Collectors.toList());
            return li;
        }
        return list;
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值