| 12345678910111213141516171819202122232425262728293031323334 |
- package cn.com.goldenwater.dcproj.utils;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Comparator;
- import java.util.List;
- public class SortList<E> {
- public List<E> sort(List<E> list, final String method, final String sort) {
- list.sort((Comparator) (a, 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;
- });
- return list;
- }
- }
|