import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class SortList<T> {
private Class<T> classType;
public SortList(Class<T> classType){
this.classType = classType;
}
public List<T> getSortList(List<T> list,final String sortFile,final String sortTYpe){
Collections.sort(list, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
Method method = getFileMethod(classType, sortFile);
try {
if(method.invoke(o1) == null || method.invoke(o2) == null){
return 0;
}
Collator collator = Collator.getInstance(Locale.CHINA);
String result1 = "";
String result2 = "";
if(method.getReturnType().getSimpleName().equals("String")){
result1 = (String)method.invoke(o1);
result2 = (String)method.invoke(o2);
if("ASC".equals(sortTYpe.toUpperCase())){
return collator.compare(result1, result2);
}
return -1*collator.compare(result1, result2);
}else{
result1 = method.invoke(o1).toString();
result2 = method.invoke(o2).toString();
if("ASC".equals(sortTYpe.toUpperCase())){
return new BigDecimal(result1).compareTo(new BigDecimal(result2));
}
return new BigDecimal(result2).compareTo(new BigDecimal(result1));
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
});
return list;
}
//獲取指定字段的get方法名
private Method getFileMethod(Class<T> clazz,String sortFile){
Method mthod = null;
try {
mthod = clazz.getMethod("get" + sortFile.substring(0,1) + sortFile.substring(1));
} catch (NoSuchMethodException | SecurityException e) {
e.printStackTrace();
}
return mthod;
}
}
對list集合中的實體類按照實體類的指定字段對實體類進行排序
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。