8d8a2debe445b0c3dd6f8b64b7066860f3d298b7.svn-base 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. package cn.com.goldenwater.dcproj.utils;
  2. import java.lang.reflect.InvocationTargetException;
  3. import java.lang.reflect.Method;
  4. import java.util.Comparator;
  5. import java.util.List;
  6. public class SortList<E> {
  7. public List<E> sort(List<E> list, final String method, final String sort) {
  8. list.sort((Comparator) (a, b) -> {
  9. int ret = 0;
  10. try {
  11. Method m1 = ((E) a).getClass().getMethod(method, null);
  12. Method m2 = ((E) b).getClass().getMethod(method, null);
  13. if (sort != null && "desc".equals(sort)) {// 倒序
  14. ret = m2.invoke(((E) b), null).toString()
  15. .compareTo(m1.invoke(((E) a), null).toString());
  16. } else {
  17. // 正序
  18. ret = m1.invoke(((E) a), null).toString()
  19. .compareTo(m2.invoke(((E) b), null).toString());
  20. }
  21. } catch (NoSuchMethodException ne) {
  22. System.out.println(ne);
  23. } catch (IllegalAccessException ie) {
  24. System.out.println(ie);
  25. } catch (InvocationTargetException it) {
  26. System.out.println(it);
  27. }
  28. return ret;
  29. });
  30. return list;
  31. }
  32. }