比较具有多个涉及布尔值的属性的列表 [英] compare list with multiple attributes involving a boolean

查看:96
本文介绍了比较具有多个涉及布尔值的属性的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类实现比较器接口,以通过添加患者对象对ArrayList进行排序,我想按多个属性对列表进行排序,而仅对Enums进行排序没有问题,但是我想通过对a进行排序来覆盖此排序布尔值.我知道我不能使用compareTo方法,因为它不是Wrapper类,但是我找不到通过boolean对列表进行排序的合适方法.

I have some classes that implement the comparator interface to sort an ArrayList by adding patient objects, I want to sort the list by multiple attributes and have no problem sorting with just Enums, however I want to override this sort by sorting with a boolean. I know I cannot use the compareTo method as it is not a Wrapper class but I am not able to find a suitable way to sort the list via boolean.

任何帮助将不胜感激.

    public Patient(int nhsNumber, String name, Status triage, boolean previouslyInQueue, boolean waitingTime){
    this.nhsNumber = nhsNumber;
    this.name = name;
    this.triage = triage;
    this.previouslyInQueue = previouslyInQueue;
    this.waitingTime = waitingTime;

}

这是我的比较器类

public class PatientInQueueComparator implements Comparator<Patient> {

@Override
public int compare(Patient p1, Patient p2) {

    if(p1.isPreviouslyInQueue() && !p2.isPreviouslyInQueue()){
        return 1;
        }else if(p1.isPreviouslyInQueue() && p2.isPreviouslyInQueue()){
        return -1;
        }
        return 0;
}

我的主要方法

List<Patient> queue = new ArrayList<Patient>();

queue.add(new Patient(1, "Bob", Status.URGENT, true, false)); //1st
queue.add(new Patient(2, "John", Status.EMERGENCY, false, false)); //5th
queue.add(new Patient(3, "Mary", Status.NON_URGENT, false, false)); //6th
queue.add(new Patient(4, "Luke", Status.SEMI_URGENT, false, true)); //4th
queue.add(new Patient(5, "Harry", Status.NON_URGENT, true, false)); //2nd
queue.add(new Patient(6, "Mark", Status.URGENT, false, true)); //3rd


System.out.println("*** Before sorting:");

for (Patient p1 : queue) {
    System.out.println(p1);
}

Collections.sort(queue, new PatientComparator( 
        new PatientInQueueComparator(),
        new PatientTriageComparator())
);

System.out.println("\n\n*** After sorting:");

for (Patient p1 : queue) {
    System.out.println(p1);
}

患者比较器

    private List<Comparator<Patient>> listComparators;

 @SafeVarargs
    public PatientComparator(Comparator<Patient>... comparators) {
        this.listComparators = Arrays.asList(comparators);
    }

@Override
public int compare(Patient p1, Patient p2) {
    for (Comparator<Patient> comparator : listComparators) {
        int result = comparator.compare(p1, p2);
        if (result != 0) {
            return result;
        }
    }
    return 0;
}

推荐答案

如果希望true的值大于false,则必须更正 您的compare()方法:

If you want true values to be greater than false, you must correct your compare() method:

@Override
public int compare(Patient p1, Patient p2) {

    if (p1.isPreviouslyInQueue() && !p2.isPreviouslyInQueue()) 
        return -1;
    if (!p1.isPreviouslyInQueue() && p2.isPreviouslyInQueue()) 
        return 1;
    return 0;
}

注意第二个if.另一个实现可能是:

notice to second if. another implementation may be :

@Override
public int compare(Patient p1, Patient p2) {
    return ((Boolean)p2.isPreviouslyInQueue()).compareTo(p1.isPreviouslyInQueue());
}

这篇关于比较具有多个涉及布尔值的属性的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆