基於Comparator對象集合實現多個條件按照優先級的比較
一、背景介紹
在日常的java開發中,我們在返回一個對象集合時需要按照對象的某個屬性或者某些屬性進行排序返回給前端進行展示,例如我最近需要返回一個題庫集合,需要先根據指定時間排序然後根據創建時間進行排序,在mysql層進行操作比較麻煩而且浪費時間,我們可以通過程序來進行排序。
二、案例代碼
// 實體類 public class People { private Integer id; private String name; private Integer topTime;// 置頂時間 private Integer gmtCreate;// 創建時間 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{ // 按照創建時間排序 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()); } } }
測試結果
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; // 當 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}]
以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。
推薦閱讀:
- C# 中的List.Sort()–集合排序方法全面解析
- 基於Arrays.sort()和lambda表達式
- Java List的sort()方法改寫compare()實現升序,降序,倒序的案例
- Java集合框架之List ArrayList LinkedList使用詳解刨析
- java對象對比之comparable和comparator的區別