My assignment is to implement the Comparable interface and use the compareTo and compareToIgnoreCase methods to sort an array of Customer objects according to their email string. This is the signature of my constructor,
public Customer(String email, String firstName, String lastName)
And this is my method,
public int compareTo(String email) {
return this.email.compareToIgnoreCase(email);
}
I understand it should return a 1, -1, or 0 based on whether the first string is greater, lesser, or equal to the second string.
Now I can't figure out how I'm supposed to use this method in sorting my array. Arrays.sort(customers) doesn't do anything.
Thanks in advance for any help you can give me.
You want to be able to sort a collection of customers, but your compareTo is comparing Customer to email. If you change it to this, the sort function should work properly:
Also ensure you implement the interface
Comparable<Customer>
in yourCustomer
class declaration.