Assume I have a user defined Java class called Foo such as:
public class Foo
{
private String aField;
@Override
public String toString()
{
I would strongly advise you to only use toString for debugging purposes... however... to expand on what Yuval A wrote above...
public class X
implements Comparator
{
public int compare(final Foo a, final Foo b)
{
return (a.toString().compareTo(b.toString()));
}
}
However you really should have Foo implement Comarable or write a proper Compartor that does not make use of toString.