基于Comparator对象集合实现多个条件按照优先级的比较

网友投稿 895 2022-12-29

基于Comparator对象集合实现多个条件按照优先级的比较

基于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-Time = 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-Time = 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{

@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 peopleList = new ArrayList(){

{

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;

thvdRKhicis.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 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() {

@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}]

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序组件实现树结构(小程序的组成结构)
下一篇:app小程序下载(复原魔方的app小程序下载)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~