if (isImplementsOf(type, Comparable.class)) {
ret = ((Comparable) m.invoke(a, null))
.compareTo((Comparable) m.invoke(b, null));
} else {
ret = String.valueOf(m.invoke(a, null)).compareTo(
String.valueOf(m.invoke(b, null)));
}
} catch (NoSuchMethodException ne) {
System.out.println(ne);
} catch (IllegalAccessException ie) {
System.out.println(ie);
} catch (InvocationTargetException it) {
System.out.println(it);
}
if (sort != null && sort.toLowerCase().equals(DESC)) {
return -ret;
} else {
return ret;
}
}
});
return list;
}
@SuppressWarnings("unchecked")
public static List<?> sortByMethod(List<?> list, final String methods[],
final String sorts[]) {
if (methods != null && methods.length > 0) {
for (int i = methods.length - 1; i >= 0; i--) {
final String method = methods[i];
String tmpSort = ASC;
if (sorts != null && sorts.length > i && sorts[i] != null) {
tmpSort = sorts[i];
}
final String sort = tmpSort;
Collections.sort(list, new Comparator() {
public int compare(Object a, Object b) {
int ret = 0;
try {
Method m = a.getClass().getMethod(method, null);
m.setAccessible(true);
Class<?> type = m.getReturnType();
if (type == int.class) {
ret = ((Integer) m.invoke(a, null))
.compareTo((Integer) m.invoke(b, null));
} else if (type == double.class) {
ret = ((Double) m.invoke(a, null))
.compareTo((Double) m.invoke(b, null));
} else if (type == long.class) {
ret = ((Long) m.invoke(a, null))
.compareTo((Long) m.invoke(b, null));
} else if (type == float.class) {
ret = ((Float) m.invoke(a, null))
.compareTo((Float) m.invoke(b, null));
} else if (type == Date.class) {
ret = ((Date) m.invoke(a, null))
.compareTo((Date) m.invoke(b, null));
} else if (isImplementsOf(type, Comparable.class)) {
ret = ((Comparable) m.invoke(a, null))
.compareTo((Comparable) m.invoke(b,
null));
} else {
ret = String.valueOf(m.invoke(a, null))
.compareTo(
String.valueOf(m
.invoke(b, null)));
}
} catch (NoSuchMethodException ne) {
System.out.println(ne);
} catch (IllegalAccessException ie) {
System.out.println(ie);
} catch (InvocationTargetException it) {
System.out.println(it);
}
if (sort != null && sort.toLowerCase().equals(DESC)) {
return -ret;
} else {
return ret;
}
}
});
}
}
return list;
}
/**
* 判断对象实现的所有接口中是否包含szInterface
*
* @param clazz
* @param szInterface
* @return
*/
public static boolean isImplementsOf(Class<?> clazz, Class<?> szInterface) {
boolean flag = false;
Class<?>[] face = clazz.getInterfaces();
for (Class<?> c : face) {
if (c == szInterface) {
flag = true;
} else {
flag = isImplementsOf(c, szInterface);
}
}
if (!flag && null != clazz.getSuperclass()) {
return isImplementsOf(clazz.getSuperclass(), szInterface);
}
return flag;
}
public static void main(String[] args) throws Exception {
List<Student> list = new ArrayList<Student>();