下午在优化别人的统计表格时发现了许多问题,原来的半成品居然没有做排序,由于取值是list类型的,以前没有接触到这类的排序,况且据我所知list本来就是有序的,一时不知道从何入手,于是在网上找了许多的资料,终于把他给搞定了。特意贴出来,以后可能用的着。至于里面的代码也没必要解释了。主要是在是使用compareto时出了问题,后来想既然他是返回boleaoo值,为何非要使用compareto呢,使用其他的比较方法一样可以返回值,于是就自己改了下。代码写的简单,但也是自己的一点进步...
public class ComparatorEc implements Comparator{
public int compare(Object arg0, Object arg1) {
ExchangeCount ec0=(ExchangeCount)arg0;
ExchangeCount ec1=(ExchangeCount)arg1;
//按总数据统计排序
int num1=Integer.parseInt(ec0.getTotalcount());
int num2=Integer.parseInt(ec1.getTotalcount());
if(num1<num2){
return 1;
}
return 0;
}
}
public void findAllExchange(DataBus dataBus) {
try {
JSONObject in = dataBus.getIn();
// ExchangeCount ec=new ExchangeCount();
List<ExchangeCount> eList = exchangeCountDao.findAllExchange();
ComparatorEc comparator=new ComparatorEc();
Collections.sort(eList, comparator);
// List<ExchangeCount> eList = exchangeCountDao.findAllExchange();
StringBuilder sb = new StringBuilder();
for(int i=0;i<eList.size();i++) {
sb.append(eList.get(i).getDate()).append(",");
sb.append(eList.get(i).getOrgName()).append(",");
sb.append(eList.get(i).getXsaj()).append(",");
sb.append(eList.get(i).getZaaj()).append(",");
sb.append(eList.get(i).getHzjq()).append(",");
sb.append(eList.get(i).getJtsj()).append(",");
sb.append(eList.get(i).getQtjq()).append(",");
sb.append(eList.get(i).getTotalcount()).append(";");
}
JSONObject msg = new JSONObject();
msg.put("mess",sb);
dataBus.setOut(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}