1,实际应用:
List<OrderInfoBean> orderInfoBeans = commitOrderDto.getValue();
SortList<OrderInfoBean> sortList = new SortList<OrderInfoBean>();
sortList.Sort(orderInfoBeans, "getCreateTime", "desc");
2,工具类sortList源码
package com.gov.util;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/***
* 用户排序
* @author huangwei
* 2015年7月1日
* @param <E>
*/
public class SortList<E> {
public void Sort(List<E> list, final String method, final String sort) {
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try {
Method m1 = ((E) a).getClass().getMethod(method, null);
Method m2 = ((E) b).getClass().getMethod(method, null);
if (sort != null && "desc".equals(sort))// 倒序
ret = m2.invoke(((E) b), null).toString()
.compareTo(m1.invoke(((E) a), null).toString());
else
// 正序
ret = m1.invoke(((E) a), null).toString()
.compareTo(m2.invoke(((E) b), null).toString());
} catch (NoSuchMethodException ne) {
System.out.println(ne);
} catch (IllegalAccessException ie) {
System.out.println(ie);
} catch (InvocationTargetException it) {
System.out.println(it);
}
return ret;
}
});
}
}
另外一种排序方式
实例:
List<CommonDictionary>list= DictionaryParam.getList("ptype");
Collections.sort(list,new SystemHWUtil. ListComparator(true,"value"));
model.addAttribute("commonDictionaries", list);
说明:list的类型是ArrayList;
按照list中的元素(CommonDictionary对象)的成员变量value进行排序
SystemHWUtil. ListComparator的源代码:
public static class ListComparator implements Comparator{
/***
* 是否转化为Int之后再比较
*/
private boolean isConvertInteger;
/***
* 对哪个列进行排序
*/
private String comparedProperty;
public ListComparator(boolean isConvertInteger,String comparedProperty) {
super();
this.isConvertInteger = isConvertInteger;
this.comparedProperty=comparedProperty;
}
public int compare(Object o1, Object o2) {
if(null!=o1&&null!=o2)
{
try {
Object obj1=ReflectHWUtils.getObjectValue(o1, comparedProperty);
Object obj2=ReflectHWUtils.getObjectValue(o2, comparedProperty);
if(isConvertInteger){
int num1;
int num2;
if(obj1 instanceof Integer){
num1=(Integer)obj1;
num2=(Integer)obj2;
}else{
num1=Integer.parseInt(obj1.toString());
num2=Integer.parseInt(obj2.toString());
}
if(num1>num2){
return 1;
}else if(num1<num2){
return -1;
}else{
return 0;
}
}else{
return obj1.toString().compareTo(obj2.toString());
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return 0/*等于*/;
}
}
附录:
public static class ArrayListComparator implements Comparator{
/***
* 排序的依据
*/
private String titles[];
/***
* 对哪个列进行排序
*/
private String comparedProperty;
public ArrayListComparator(String[] titles,String comparedProperty) {
super();
this.titles = titles;
this.comparedProperty=comparedProperty;
}
public int compare(Object o1, Object o2) {
if(null!=o1&&null!=o2)
{
try {
if(SystemHWUtil.indexOfArr(titles,(String)ReflectHWUtils.getObjectValue(o1, comparedProperty) ) >
SystemHWUtil.indexOfArr(titles,(String)ReflectHWUtils.getObjectValue(o2, comparedProperty))){
return 1/*大于*/;
}else {
return -1/*小于*/;
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return 0/*等于*/;
}
}