public class ListSort<T>{ Collections.sort(list new Comparator(){ public int compare(Object a,Object b){ int num =0; try{ Method m1 = ((T)a).getClass().getMethod(method, null); Method m2 =((T)b).getClass().getMethod(method, null); if(sort != null && "desc".equals(sort))//倒序 num = m2.invoke(((T)b), null).toString().compareTo(m1.invoke(((T)a), null).toString()); else//正序 num = m1.invoke(((T)a), null).toString().compareTo(m2.invoke(((T)b), null).toString()); }catch(NoSuchMethodException e){ System.out.println(e); }catch(IllegalAccessException e1){ System.out.println(e1); }catch(InvocationTargetException e2){ System.out.println(e2); } return num; } }) }
时间: 2024-10-11 11:42:25