Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare list with multiple attributes involving a 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.

Any help would be greatly appreciated.

    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;

}

This is my comparator 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;
}

My main method

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);
}

Patient Comparator

    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;
}
like image 821
cg91 Avatar asked Mar 16 '23 08:03

cg91


2 Answers

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;
}

notice to second if. another implementation may be :

@Override
public int compare(Patient p1, Patient p2) {
    return ((Boolean)p2.isPreviouslyInQueue()).compareTo(p1.isPreviouslyInQueue());
}
like image 85
Taher Khorshidi Avatar answered Apr 08 '23 02:04

Taher Khorshidi


You just need to implement the appropriate logic in your compare method.

@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
   }
like image 45
fdsa Avatar answered Apr 08 '23 02:04

fdsa