比较具有涉及布尔值的多个属性的列表
compare list with multiple attributes involving a boolean
我有一些 classes 实现比较器接口以通过添加患者对象对 ArrayList 进行排序,我想按多个属性对列表进行排序并且仅使用枚举进行排序没有问题,但是我想通过用布尔值排序来覆盖这种排序。我知道我不能使用 compareTo
方法,因为它不是 Wrapper class,但我无法找到合适的方法来通过 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;
}
这是我的比较器class
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;
}
您只需在 compare
方法中实现适当的逻辑。
@Override
public int compare(Patient p1, Patient p2) {
if(p1.isPreviouslyInQueue() && !p2.isPreviouslyInQueue()){
return 1;
}else if(p1.isPreviouslyInQueue() && p2.isPreviouslyInQueue()){
return -1;
}
//instead of returning zero here, sort by your next criteria
}
如果您希望 true
值大于 false
,您必须更正
你的 compare()
方法:
@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
。另一个实现可能是:
@Override
public int compare(Patient p1, Patient p2) {
return ((Boolean)p2.isPreviouslyInQueue()).compareTo(p1.isPreviouslyInQueue());
}
我有一些 classes 实现比较器接口以通过添加患者对象对 ArrayList 进行排序,我想按多个属性对列表进行排序并且仅使用枚举进行排序没有问题,但是我想通过用布尔值排序来覆盖这种排序。我知道我不能使用 compareTo
方法,因为它不是 Wrapper class,但我无法找到合适的方法来通过 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;
}
这是我的比较器class
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;
}
您只需在 compare
方法中实现适当的逻辑。
@Override
public int compare(Patient p1, Patient p2) {
if(p1.isPreviouslyInQueue() && !p2.isPreviouslyInQueue()){
return 1;
}else if(p1.isPreviouslyInQueue() && p2.isPreviouslyInQueue()){
return -1;
}
//instead of returning zero here, sort by your next criteria
}
如果您希望 true
值大于 false
,您必须更正
你的 compare()
方法:
@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
。另一个实现可能是:
@Override
public int compare(Patient p1, Patient p2) {
return ((Boolean)p2.isPreviouslyInQueue()).compareTo(p1.isPreviouslyInQueue());
}