```Map<String,Object> map = new HashMap<>();
map.put("001","张三");
map.put("002","李四");
map.put("003","王五");
List<MyMoney> moneyList = new ArrayList<MyMoney>();
MyMoney money1 = new MyMoney();
money1.setCode("001");money1.setMoney(100);
moneyList.add(money1);
MyMoney money2 = new MyMoney();
money2.setCode("001");money2.setMoney(80);
moneyList.add(money2);
MyMoney money3 = new MyMoney();
money3.setCode("002");money3.setMoney(90);
moneyList.add(money3);
MyMoney money4 = new MyMoney();
money4.setCode("003");money4.setMoney(60);
moneyList.add(money4);
MyMoney money5 = new MyMoney();
money5.setCode("002");money5.setMoney(30);
moneyList.add(money5);
Collections.sort(moneyList, new Comparator<MyMoney>() {
@Override
public int compare(MyMoney o1, MyMoney o2) {
int m1 = o1.getMoney();
int m2 = o2.getMoney();
if (m1 > m2) {
return 1;
} else if (m1 == m2) {
return 0;
} else {
return -1;
}
}
});
for(int i=0;i<moneyList.size();i++)
{
MyMoney news=(MyMoney)moneyList.get(i);
System.out.println("code:"+map.get(news.getCode())+" money:"+news.getMoney()+" 排名"+(i+1)+"\r\n");
}
下面是MyMoney对象类
public class MyMoney {
private String code;
private Integer money;
public MyMoney(){}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Integer getMoney() {
return money;
}
public void setMoney(Integer money) {
this.money = money;
}
}
代码可以直接拿来用,我已测过,望采纳
这是执行结果:
code:李四 money:30 排名1
code:王五 money:60 排名2
code:张三 money:80 排名3
code:李四 money:90 排名4
code:张三 money:100 排名5