Search code examples
javaobjectarraylistoverridingcompareto

Java: Implementing Comparable Issue


I'm new here and was looking for some advise on my code. I'm trying to make it possible for an arrayList ArrayList<patient> heartPatientArray = new ArrayList<patient>(); //heart patients to be able to be sorted on a patient's arrivalTime.

Code:

    class patient implements Comparable
{
    int typeSickness; //1 for heart 2 for gastro 3 for bleeding
    double arrivalTime; //what time they arrived   SORT BY
    int deathTime; // what time they are set to balk or die
    int status; //0 for being cared for, 1 to n for location in line, -1 if dead
    int personalNumberInLine; //personal number in line updated everytime someone enters the line
    double timeSpentInQueue; //total time before they either died or were able to be treated
    int idOfPerson; //unique id the person gets when entering the queue
    boolean isAlive;

    public patient(int sickness, double arrival, int ID) { //sets the patients sickness, time arrived, and ID
        typeSickness=sickness;
        arrivalTime=arrival;
        idOfPerson = ID;
    }
    public int getTypeSickness() {
        return typeSickness;
    }
    public void setTypeSickness(int typeSickness) {
        this.typeSickness = typeSickness;
    }
    public double getArrivalTime() {
        return arrivalTime;
    }
    public void setArrivalTime(double arrivalTime) {
        this.arrivalTime = arrivalTime;
    }
    public int getDeathTime() {
        return deathTime;
    }
    public void setDeathTime(int deathTime) {
        this.deathTime = deathTime;
    }

    public int getStatus() {
        return status;
    }
    public void setStatus(int status) {
        this.status = status;
    }
    public int getNumberInLine() {
        return personalNumberInLine;
    }
    public void setNumberInLine(int numberInLine) {
        this.personalNumberInLine = numberInLine;
    }
    @Override
    public int compareTo(patient one) {
        if (this.getArrivalTime() < one.getArrivalTime()) {
            return -1;
        }
        else if(this.getArrivalTime() > one.getArrivalTime()){
            return 1;
        }

        return 0;
    }

some code edited out for time's sake

I've tried styling it after some code I found online but I'm getting an error on compareTo(patient one) of Remove Override Notation, and an error on class patient implements Comparable telling me I must make the class abstract.

After I implement the compareTo correctly, how would I go about sorting the heartPatientArray?


Solution

  • You are implementing the raw form of the Comparable interface. Because of this, compareTo takes an Object, which explains why you are getting an error attempting to implement the interface.

    Instead, pass your class as the type parameter.

    class patient implements Comparable<patient>
    

    Then your compareTo method, as is, will implement Comparable<patient> properly.

    Typically, Java naming conventions would say to capitalize your class name.

    class Patient implements Comparable<Patient>
    

    You can sort your list with:

    Collections.sort(heartPatientArray);
    

    If you'd like to sort it in reverse order, you can specify that with:

    Collections.sort(heartPatientArray, Comparator.reverseOrder());
    

    In general, you can sort it however you want by passing an instance of Comparator<Patient>, implementing Comparator's compare method, taking care to pass a type parameter to Comparable like we are now doing for Comparable. Then pass an instance of Comparator<Patient> as the second argument to Collections.sort.