QUOTE(soltan @ 1 May, 2009 - 01:15 AM)

Hi, I知 having a problem using the compareTo method. The compareTo method is used to store strings in alphabetical order of last name then first name if last name is the same.
That is the interface
CODE
public interface Comparable<T>
{
int compareTo(T rhs);
}
The implementation
CODE
public int compareTo(Object rhs)
{
int compare = theLastName().compareTo(rhs.theLastName());
return (compare !=0 ? compare : theFirstName().compareTo(rhs.theFirstName()));
}
So as you can see I知 using objects here and I知 using an arrayList to store them , How do i employ my compareTo method and using it to sort my collection with Collection.sort(theArrayList) in my main class ?
Thanks
What is T ?
If T is an Object : class Rhs implements Comparable<Object>
then you cannot do:
rhs.theLastName()); because rhs is an Object and Object do not have a method called "theLastName" so you will have to cast
int compareTo(Object o) {
Rsh rsh = (Rhs) o;
... continue as you did
Now if T is and Rhs: class Rhs implements Comparable<Rhs>
then you'll have to change the signature of your method from
int compareTo(Object rhs)
to
int compareTo(Rhs rhs)
hope this hekps