List使用泛型的通用排序
public class SortList<E>{ /** * @param list 需要被排序的list * @param method 某属性字段的getter方法,根据该字段排序 * @param sort desc倒序,asc正序 * */ public void Sort(List<E> list, final String method, final String sort){ Collections.sort(list, new Comparator() { @Override 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; } }); } }