Java 中多条件排序

时间:2021-04-22 03:02:50

Collections.sort(ghEntityList, new Comparator<GongHuiEntity>() {
  @Override
  public int compare(GongHuiEntity o1, GongHuiEntity o2) {
  if (o1 == null || o2 == null) {
    return 0;
  }  
  //第一次比较等级
  int i = o2.getGongHuiModel().getLevel() - o1.getGongHuiModel().getLevel();
  //如果等级相同比较经验
  if(i == 0){
    int o1Exp = o1.getGongHuiModel().getExp();
    int o2Exp = o2.getGongHuiModel().getExp();
    int j = o2Exp - o1Exp;
    return j;
  }
  return i;
  }
});