I have an array of Appointment objects that I would like to sort based on a few field.
Appointment class with overridden compareTo
public class Appointment implements Comparable<Appointment>{
String month;
String day;
int hour;
int minute;
String description;
// constructor, getters and setters omitted to emphasize Comparable implementation
@Override
public int compareTo(Appointment other){
if (this.month.compareTo(other.month) == 0){
return 0;
} else if (this.month.compareTo(other.month) > 0){
return 1;
} else if (this.month.compareTo(other.month) < 0){
return -1;
}
if (this.day.compareTo(other.day) == 0){
return 0;
} else if (this.day.compareTo(other.day) > 0){
return 1;
} else if (this.day.compareTo(other.day) < 0){
return -1;
}
if (this.hour == other.hour){
return 0;
} else if (this.hour > other.hour){
return 1;
} else if (this.hour < other.hour){
return -1;
}
if (this.minute == other.minute){
return 0;
} else if (this.minute > other.minute){
return 1;
} else if (this.minute < other.minute){
return -1;
}
return 0;
}
}
Issue
In my main class, named Calendar, I create and save a number of Appointment objects to the array bookings.
However I cannot sort the list with the compareTo method I defined.
Collections.sort(bookings, new Appointment());
This gives me the error:
No suitable method found for sort(object[],Appointment)
Any idea how I could solve this?