有時(shí)候需要對(duì)List對(duì)象進(jìn)行排序,如果每一處都去寫一個(gè)排序方法,就會(huì)產(chǎn)生重復(fù)代碼的壞味道,而且每一處都寫,工作量會(huì)很大。
我們知道,Java提供了一個(gè)Collections.sort()方法可以對(duì)List排序,利用Java反射機(jī)制,很容易就能寫出一個(gè)通用的排序方法。
為了防止出現(xiàn)不按照getter,setter規(guī)范命名的POJO類,我不打算動(dòng)態(tài)調(diào)用getXXX()方法,而是直接獲取對(duì)象的屬性值。為了達(dá)到不論是否是public成員變量,都能獲取到的目的,在獲取到Field后,調(diào)用了setAccessible(true); 來設(shè)置訪問權(quán)限。
具體代碼如下:
[java] view plain copy 在CODE上查看代碼片 派生到我的代碼片
/**
* 對(duì)List對(duì)象按照某個(gè)成員變量進(jìn)行排序
* @param list List對(duì)象
* @param sortField 排序的屬性名稱
* @param sortMode 排序方式:ASC,DESC 任選其一
*/
public static <T> void sortList(List<T> list, final String sortField, final String sortMode) {
Collections.sort(list, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
try {
Class clazz = o1.getClass();
Field field = clazz.getDeclaredField(sortField); //獲取成員變量
field.setAccessible(true); //設(shè)置成可訪問狀態(tài)
String typeName = field.getType().getName().toLowerCase(); //轉(zhuǎn)換成小寫
Object v1 = field.get(o1); //獲取field的值
Object v2 = field.get(o2); //獲取field的值
boolean ASC_order = (sortMode == null || "ASC".equalsIgnoreCase(sortMode));
//判斷字段數(shù)據(jù)類型,并比較大小
if(typeName.endsWith("string")) {
String value1 = v1.toString();
String value2 = v2.toString();
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("short")) {
Short value1 = Short.parseShort(v1.toString());
Short value2 = Short.parseShort(v2.toString());
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("byte")) {
Byte value1 = Byte.parseByte(v1.toString());
Byte value2 = Byte.parseByte(v2.toString());
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("char")) {
Integer value1 = (int)(v1.toString().charAt(0));
Integer value2 = (int)(v2.toString().charAt(0));
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("int") || typeName.endsWith("integer")) {
Integer value1 = Integer.parseInt(v1.toString());
Integer value2 = Integer.parseInt(v2.toString());
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("long")) {
Long value1 = Long.parseLong(v1.toString());
Long value2 = Long.parseLong(v2.toString());
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("float")) {
Float value1 = Float.parseFloat(v1.toString());
Float value2 = Float.parseFloat(v2.toString());
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("double")) {
Double value1 = Double.parseDouble(v1.toString());
Double value2 = Double.parseDouble(v2.toString());
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("boolean")) {
Boolean value1 = Boolean.parseBoolean(v1.toString());
Boolean value2 = Boolean.parseBoolean(v2.toString());
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("date")) {
Date value1 = (Date)(v1);
Date value2 = (Date)(v2);
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else if(typeName.endsWith("timestamp")) {
Timestamp value1 = (Timestamp)(v1);
Timestamp value2 = (Timestamp)(v2);
return ASC_order ? value1.compareTo(value2) : value2.compareTo(value1);
}
else {
//調(diào)用對(duì)象的compareTo()方法比較大小
Method method = field.getType().getDeclaredMethod("compareTo", new Class[]{field.getType()});
method.setAccessible(true); //設(shè)置可訪問權(quán)限
int result = (Integer)method.invoke(v1, new Object[]{v2});
return ASC_order ? result : result*(-1);
}
}
catch (Exception e) {
String err = e.getLocalizedMessage();
System.out.println(err);
e.printStackTrace();
}
return 0; //未知類型,無法比較大小
}
});
}
更多信息請(qǐng)查看IT技術(shù)專欄