利用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);
}
}