/** * @param list 要排序的list * @param field 排序的属性 * @param sort desc:倒叙 asc:正序 */ @SuppressWarnings("unchecked") public void sortList(List<AnalysisPrame> list, final String field, final String sort){ Collections.sort(list, new Comparator() { public int compare(Object a, Object b) { int ret = 0; String method="get"+field.substring(0, 1).toUpperCase()+field.substring(1); try{ Method m1 = ((AnalysisPrame)a).getClass().getMethod(method); Method m2 = ((AnalysisPrame)b).getClass().getMethod(method); if(sort != null && "desc".equals(sort))//倒序 ret = m2.invoke(((AnalysisPrame)b)).toString().compareTo(m1.invoke(((AnalysisPrame)a)).toString()); else if(sort != null && "asc".equals(sort))//正序 ret = m1.invoke(((AnalysisPrame)a)).toString().compareTo(m2.invoke(((AnalysisPrame)b)).toString()); }catch(NoSuchMethodException e){ e.printStackTrace(); }catch(IllegalAccessException e){ e.printStackTrace(); }catch(InvocationTargetException e){ e.printStackTrace(); } return ret; } }); }