-3

What can I replace the < and > with because i want to compare string below one is compare int, so got any solution to solve this question and doesnt change the function.

public int compareTo(Object o) {
    Patient p = (Patient) o;
    if (this.getCategory() < p.getCategory())
        return -1;
    if (this.getCategory() > p.getCategory())
        return 1;
    else { 
        if (this.getTimeArrived().before(p.getTimeArrived()))
            return -1;
        if (this.getTimeArrived().after(p.getTimeArrived()))
            return 1;
    }
    return 0;
}

How about this? can change the > & < to another solution because i want to compare with string

    import java.util.Comparator;

public class PatientComparator implements Comparator<Patient>{
    public int compare(Patient p1, Patient p2) {
    if (p1.getCategory() < p2.getCategory())
        return -1;
    if (p1.getCategory() > p2.getCategory())
        return 1;
    else { if (p1.getTimeArrived().before(p2.getTimeArrived()))
        return -1;
           if (p1.getTimeArrived().after(p2.getTimeArrived()))
        return 1;
    }
    return 0;

}

}
John Lew
  • 1
  • 1
  • 2
    What does `getCategory()` and `getTimeArrived()` return? – Slaw Aug 18 '18 at 14:47
  • Please add a complete description to the problem you are facing, its bit confusing – Swapnil Aug 18 '18 at 14:51
  • Don't post additional information as an answer (or comment). Instead, [edit] your question. – Slaw Aug 18 '18 at 14:59
  • "if (p1.getCategory() == p2.getCategory()) return -1;" doesn't look right for two reasons: (1) if two objects are considered equal then result of comparision should be `0`; (2) strings shouldn't be compared with `==` (more info: [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)) – Pshemo Aug 18 '18 at 15:32
  • i already change the question just now i put wrong code – John Lew Aug 18 '18 at 15:35

1 Answers1

1

From the additional information you provided (currently inside an answer) getCategory() returns a String and getTimeArrived() returns a java.util.Date. Your goal seems to be: Compare by "category" and, if equal, then compare by "time arrived".

Both String and Date implement the Comparable interface, so you can do something like this:

public int compareTo(Patient other) {
    // This code doesn't handle nulls
    int result = getCategory().compareTo(other.getCategory());
    if (result == 0) {
        result = getTimeArrived().compareTo(other.getTimeArrived());
    }
    return result;
}

You could also create a Comparator.

Comparator<Patient> c = Comparator.comparing(Patient::getCategory)
        .thenComparing(Patient::getArrivedTime);

Also, you are creating a compareTo method without Patient implementing Comparable. You should change it to:

public class Patient implements Comparable<Patient> { /* code */ }

Then override the compareTo method declared in Comparable. This also forces you to use compareTo(Patient) rather than compareTo(Object).

Slaw
  • 37,820
  • 8
  • 53
  • 80
  • @JohnLew If your other question is about using `==`... replace `==` with [`equals`](https://docs.oracle.com/javase/10/docs/api/java/lang/Object.html#equals(java.lang.Object)). Otherwise, post another, separate question. – Slaw Aug 18 '18 at 15:25