|
@@ -3,6 +3,8 @@ package com.diagbot.util;
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -96,11 +98,11 @@ public class ReflectUtil {
|
|
|
* @param property
|
|
|
* @param value
|
|
|
*/
|
|
|
- public static void setFieldValue(Object object, String property, Object value){
|
|
|
+ public static void setProperty(Object object, String property, Object value) {
|
|
|
//根据 对象和属性名通过反射 调用上面的方法获取 Field对象
|
|
|
- Field field = getDeclaredField(object, property) ;
|
|
|
+ Field field = getDeclaredField(object, property);
|
|
|
//抑制Java对其的检查
|
|
|
- field.setAccessible(true) ;
|
|
|
+ field.setAccessible(true);
|
|
|
try {
|
|
|
field.set(object, value);
|
|
|
} catch (IllegalArgumentException e) {
|
|
@@ -110,6 +112,32 @@ public class ReflectUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据指定字段按照字符串排序
|
|
|
+ *
|
|
|
+ * @param tList
|
|
|
+ * @param property
|
|
|
+ * @param <T>
|
|
|
+ */
|
|
|
+ public <T> void sort(List<T> tList, String property) {
|
|
|
+ if (ListUtil.isNotEmpty(tList) && tList.size() > 1) {
|
|
|
+ Collections.sort(tList, new Comparator<T>() {
|
|
|
+ @Override
|
|
|
+ public int compare(T o1, T o2) {
|
|
|
+ String v1 = getProperty(o1, property);
|
|
|
+ String v2 = getProperty(o2, property);
|
|
|
+ if (StringUtil.isBlank(v1)) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (StringUtil.isBlank(v2)) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ return v1.compareTo(v2);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
}
|