java 工具类sort_Java 通用排序工具类ListSortUtils
場(chǎng)景:Java 類 需 重新排序,有時(shí)升序、有時(shí)倒敘,有時(shí)是多字段排序
代碼:
package GenericTest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* [簡(jiǎn)述]:List通用排序工具類
* User: xyj
* Date: 2016/07/03
* Created with IntelliJ IDEA.
*/
public class ListSortUtils {
private static final String SORT_ASC = "asc";
private static final String SORT_DESC = "desc";
/**
* [簡(jiǎn)述]: 對(duì)List數(shù)組排序
* @param list 源數(shù)據(jù) 排序集合
* @param sort 升序 還是 降序,默認(rèn)升序
* @return List
*/
public static List> sort(List> list,final String sort){
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int ret = String.valueOf(o1).compareTo(String.valueOf(o2));;
if(null != sort && SORT_DESC.equalsIgnoreCase(sort)){
return -ret;
}else{
return ret;
}
}
});
return list;
}
/**
*[簡(jiǎn)述]: List 泛型 排序
* @param list 源數(shù)據(jù) 排序集合
* @param field 排序的數(shù)據(jù)字段名稱
* @param sort 升序 還是 降序,默認(rèn)升序
* @param 泛型T
* @return List
*/
public static List sort(List list,final String field,final String sort){
Collections.sort(list, new Comparator() {
@Override
public int compare(T o1, T o2) {
int ret = 0;
try {
Method method1 = o1.getClass().getDeclaredMethod(getMethodName(field),null);
Method method2 = o1.getClass().getDeclaredMethod(getMethodName(field), null);
ret = method1.invoke(o1).toString().compareTo( method2.invoke(o2).toString());
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if(null != sort && SORT_DESC.equalsIgnoreCase(sort)){
return -ret;
}else{
return ret;
}
}
});
return list;
}
/**
*[簡(jiǎn)述]: List 泛型 排序
* @param list 源數(shù)據(jù) 排序集合
* @param fields 排序的數(shù)據(jù)字段名稱
* @param sorts 升序 還是 降序
* @param 泛型T
* @return List
*/
public static List sort(List list,final String [] fields,final String [] sorts){
if(null != fields && fields.length > 0){
for(int index = 0;index < fields.length;index ++){
String sortRule = SORT_ASC;
if(null != sorts && sorts.length >= index && null != sorts[index]){
sortRule = sorts[index];
}
final String sort = sortRule;
final String field = fields[index];
Collections.sort(list, new Comparator() {
@Override
public int compare(T o1, T o2) {
int ret = 0;
try {
Method method1 = o1.getClass().getDeclaredMethod(getMethodName(field),null);
Method method2 = o1.getClass().getDeclaredMethod(getMethodName(field), null);
ret = method1.invoke(o1).toString().compareTo( method2.invoke(o2).toString());
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if(null != sort && SORT_DESC.equalsIgnoreCase(sort)){
return -ret;
}else{
return ret;
}
}
});
}
}
return list;
}
private static String getMethodName(String str){
StringBuffer name = new StringBuffer();
name = name.append("get").append(firstLetterToCapture(str));
return name.toString();
}
private static String firstLetterToCapture(String name){
char[] arr = name.toCharArray();
arr[0] -= 32;
return String.valueOf(arr);
}
}
總結(jié)
以上是生活随笔為你收集整理的java 工具类sort_Java 通用排序工具类ListSortUtils的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html checked属性值,HTML
- 下一篇: Linux lua 性能,systemT