基于Comparator對象集合實(shí)現(xiàn)多個條件按照優(yōu)先級的比較
更新時間:2021年07月12日 09:25:41 作者:CoderYin
這篇文章主要介紹了基于Comparator對象集合實(shí)現(xiàn)多個條件按照優(yōu)先級的比較,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
一、背景介紹
在日常的java開發(fā)中,我們在返回一個對象集合時需要按照對象的某個屬性或者某些屬性進(jìn)行排序返回給前端進(jìn)行展示,例如我最近需要返回一個題庫集合,需要先根據(jù)指定時間排序然后根據(jù)創(chuàng)建時間進(jìn)行排序,在mysql層進(jìn)行操作比較麻煩而且浪費(fèi)時間,我們可以通過程序來進(jìn)行排序。
二、案例代碼
// 實(shí)體類
public class People {
private Integer id;
private String name;
private Integer topTime;// 置頂時間
private Integer gmtCreate;// 創(chuàng)建時間
public People(Integer id, String name, Integer topTime, Integer gmtCreate) {
super();
this.id = id;
this.name = name;
this.topTime = topTime;
this.gmtCreate = gmtCreate;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getTopTime() {
return topTime;
}
public void setTopTime(Integer topTime) {
this.topTime = topTime;
}
public Integer getGmtCreate() {
return gmtCreate;
}
public void setGmtCreate(Integer gmtCreate) {
this.gmtCreate = gmtCreate;
}
@Override
public String toString() {
return "People [id=" + id + ", name=" + name + ", topTime=" + topTime + ", gmtCreate=" + gmtCreate + "]";
}
}
// 排序方法
public class PeopleComparator implements Comparator<People>{
@Override
public int compare(People o1, People o2) {
int result = 0;
// 按照置頂時間排序升序(o1,o2位置互換就是降序)
int topTimeSeq = o2.getTopTime() - o1.getTopTime();
if(topTimeSeq != 0){
result = (topTimeSeq > 0) ? 3 : -1;
}else{
// 按照創(chuàng)建時間排序
topTimeSeq = o2.getGmtCreate() - o1.getGmtCreate();
if(topTimeSeq != 0){
result = (topTimeSeq > 0) ? 2 : -2;
}
}
return result;
}
}
// 測試
public class PeopleTest {
public static void main(String[] args) {
List<People> peopleList = new ArrayList<People>(){
{
add(new People(1,"tom1",0,1));
add(new People(2,"tom2",2,4));
add(new People(3,"tom3",1,3));
add(new People(4,"tom4",0,6));
add(new People(5,"tom5",0,2));
add(new People(6,"tom6",0,5));
}
};
Collections.sort(peopleList,new PeopleComparator());
for(People p:peopleList){
System.out.println(p.toString());
}
}
}
測試結(jié)果

Comparator 多條件比較
class Card {
int a;
int b;
public Card(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
@Override
public String toString() {
return "Card{" +
"a=" + a +
", b=" + b +
'}';
}
}
public class Main {
public static void main(String[] args) {
List<Card> list = new ArrayList<>();
list.add(new Card(0, 2));
list.add(new Card(1, 1));
list.add(new Card(1, 0));
list.add(new Card(1, 0));
list.add(new Card(2, 0));
System.out.println(list);
System.out.println();
Collections.sort(list, new Comparator<Card>() {
@Override
public int compare(Card c1, Card c2) {
// c1 - c2 升序
// c2 - c1 降序
int res1 = c2.b - c1.b;
int res2 = c2.a - c1.a;
// 當(dāng) b相等時比較a, 否則先比較b
return res1 == 0 ? res2 : res1;
}
});
System.out.println(list);
}
}
[Card{a=0, b=2}, Card{a=1, b=1}, Card{a=1, b=0}, Card{a=1, b=0}, Card{a=2, b=0}]
[Card{a=0, b=2}, Card{a=1, b=1}, Card{a=2, b=0}, Card{a=1, b=0}, Card{a=1, b=0}]
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解使用Spring的restTemplete進(jìn)行Http請求
本篇文章主要介紹了詳解使用Spring的restTemplete進(jìn)行Http請求,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(27)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07

