如何用java实现sql里多字段排序, 如order by col1 desc, col2, col3 desc…
奥运会时期,每场赛事后,对国家的金、银、铜牌进行排序,排序规则如下:
先以金牌排序,金牌数越多,排得越前,如果金牌数相同,则比银牌数,如果银牌数一样多,则比铜牌数,如果铜牌数还一样多,则按国家英文字母顺序升序排序。
例如:
China 51 20 21
American 50 1 1
Japan 0 0 0
上面是三个国家的奖牌数,每一行依次是国家名、金牌数、银牌数、铜牌数。
public class Medal {
private String country; // 国家
private int gi; // 金牌数量
private int si; // 银牌数量
private int bi; // 铜牌数量
public Medal() {
}
public Medal(String country, int gi, int si, int bi) {
this.country = country;
this.gi = gi;
this.si = si;
this.bi = bi;
}
public String getCountry() {
return country;
}
public int getGi() {
return gi;
}
public int getSi() {
return si;
}
public int getBi() {
return bi;
}
}
/**
* 使用单个比较器进行多字段排序
* @param medalList
* @return
*/
public static List rankBySingleComparator(List medalList){
Collections.sort(medalList,(medal1,medal2)->{
//第一排序字段
if(medal1.getGi()!=medal2.getGi()){
return medal2.getGi()-medal1.getGi(); //若要正序,交互medal1和medal2的位置即可
}
//第二排序字段
if(medal1.getSi()!=medal2.getSi()){
return medal2.getSi()-medal1.getSi(); //若要正序,交互medal1和medal2的位置即可
}
//第三排序字段
if(medal1.getBi()!=medal2.getBi()){
return medal2.getBi()-medal1.getBi(); //若要正序,交互medal1和medal2的位置即可
}
//第四排序字段
return medal1.getCountry().compareTo(medal1.getCountry()); //若要倒序,交互medal1和medal2的位置即可
});
return medalList.stream().map(Medal::getCountry).collect(Collectors.toList());
}
/**
* 使用Stream进行多字段排序
*
* @param medalList
* @return
*/
public static List rankByStream(List medalList) {
return medalList.stream().sorted(Comparator.comparing(Medal::getGi, Comparator.reverseOrder())
.thenComparing(Medal::getSi, Comparator.reverseOrder())
.thenComparing(Medal::getBi, Comparator.reverseOrder())
.thenComparing(Medal::getCountry)).map(Medal::getCountry).collect(Collectors.toList());
}
public static void main(String[] args) {
List list1 = new ArrayList<>();
list1.add(new Medal("China",51,20,21));
list1.add(new Medal("American",50,1,1));
list1.add(new Medal("Japan",0,0,0));
List expected1 = Arrays.asList("China", "American", "Japan");
Assert.isTrue(expected1.equals(rankByStream(list1)),"rankByStream error");
Assert.isTrue(expected1.equals(rankBySingleComparator(list1)),"rankByStream error");
List list2 = new ArrayList<>();
list2.add(new Medal("China",51,20,21));
list2.add(new Medal("American",52,1,1));
list2.add(new Medal("Japan",0,0,0));
List expected2 = Arrays.asList("American", "China", "Japan");
Assert.isTrue(expected2.equals(rankByStream(list2)),"rankByStream error");
Assert.isTrue(expected2.equals(rankBySingleComparator(list2)),"rankByStream error");
List list3 = new ArrayList<>();
list3.add(new Medal("China",51,20,21));
list3.add(new Medal("American",51,20,22));
list3.add(new Medal("Japan",53,0,0));
List expected3 = Arrays.asList("Japan", "American", "China");
Assert.isTrue(expected3.equals(rankByStream(list3)),"rankByStream error");
Assert.isTrue(expected3.equals(rankBySingleComparator(list3)),"rankByStream error");
List list4 = new ArrayList<>();
list4.add(new Medal("China",51,20,21));
list4.add(new Medal("American",50,1,1));
list4.add(new Medal("Japan",0,0,0));
list4.add(new Medal("France",51,20,20));
List expected4 = Arrays.asList("China", "France", "American", "Japan");
Assert.isTrue(expected4.equals(rankByStream(list4)),"rankByStream error");
Assert.isTrue(expected4.equals(rankBySingleComparator(list4)),"rankByStream error");
}
参考
[1]: https://www.cnblogs.com/qujiayuan/p/12902380.html