举例说明:
现在有一个list集合rclistday。
实体中有字段:
private int ranking; //排名
private Double sal_amt; //成交金额
下面是排序代码,修改list名称和你要根据什么字段排名的名称,就可直接使用(注,list的顺序需要是根据要排名的字段呈倒序)
int r = 1;
for (int j = 0; j < rclistday.size(); j++) {
if (j<rclistday.size()-1) {
if (rclistday.get(j).getSal_qty() == rclistday.get(j+1).getSal_qty()) {
rclistday.get(j).setRanking(r);
}else if(rclistday.get(j).getSal_qty() > rclistday.get(j+1).getSal_qty()){
rclistday.get(j).setRanking(r);
r++;
}
}else if(j == rclistday.size()-1 && rclistday.size() > 1){
if(rclistday.get(j).getSal_qty() == rclistday.get(j-1).getSal_qty()){
rclistday.get(j).setRanking(r);
}else if(rclistday.get(j).getSal_qty() < rclistday.get(j-1).getSal_qty()){
rclistday.get(j).setRanking(r);
}else{
rclistday.get(j).setRanking(r+1);
}
}
if(rclistday.size() == 1){
rclistday.get(j).setRanking(1);
}
}