以下是我采用的方法。 它对比较“强项”做了罚款。 我也希望能够比较对日期或可能具有可接受的定义的任何类别。 方法。 寻找一种容易的办法。 我正走一条ck路。 还可以接受任何其他改进这一方法的建议。
protected <E> int compareFields(E o1, E o2,String fieldName){
String o1Data;
String o2Data;
try {
o1Data = (String) o1.getClass().getMethod(fieldName).invoke(o1);
o2Data = (String) o2.getClass().getMethod(fieldName).invoke(o2);
}
catch(Exception e) {
throw new RuntimeException(e);
}
if(o1Data == null && o2Data == null){
return 0;
} else if (o1Data == null){
return 1;
} else if (o2Data == null){
return -1;
}
return o2Data.compareTo(o1Data);
}