研究了一下,,自定义了一个 comparator 搞定了。。
public class GoogleReportComparator implements Comparator<CampaignVO> {
private ArrayList<String> fieldList;
public GoogleReportComparator(ArrayList<String> fieldList) {
this.fieldList = fieldList;
}
@
Override public int compare(CampaignVO obj1, CampaignVO obj2) {
int compare = 0;
for (int i = 0; i < fieldList.size(); i++) {
compare = getCompare(obj1, obj2, i);
if (compare != 0) {
return compare;
}
}
return compare;
}
private int getCompare(CampaignVO obj1, CampaignVO obj2, int i) {
String filedValue1 = getCurrnetFiledValue(obj1, fieldList.get(i));
String filedValue2 = getCurrnetFiledValue(obj2, fieldList.get(i));
return filedValue1.compareTo(filedValue2);
}
private String getCurrnetFiledValue(CampaignVO campaignVO, String fieldName) {
StringBuffer sb = new StringBuffer();
sb.append("get");
sb.append(fieldName.substring(0, 1).toUpperCase());
sb.append(fieldName.substring(1));
try {
Method method = campaignVO.getClass().getMethod(sb.toString());
Object obj = method.invoke(campaignVO);
if (null == obj) {
return StringUtils.EMPTY;
} else {
return obj.toString();
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
log.error("invoke method error::", e);
}
return StringUtils.EMPTY;
}
}