importjava.util.Collections;importjava.util.Comparator;importjava.util.List;importjava.lang.reflect.Method;importjava.lang.reflect.InvocationTargetException;/***@authorja...
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
* @author jardot
* @version 1.0
* 通用排序
*/
public class SortList{
public void Sort(List list, final String method, final String sort){
Collections.sort(list, new Comparator() {
public int compare(Object a, Object 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;
}
});
}
}
ret = m1.invoke(((E)a), null).toString().compareTo(m2.invoke(((E)b), null).toString());这条代码怎么就能够为list重新排序呢?在下小鸟鸟,望各位大大指教!
展开