I searched for this question, but I only found one thread that was kind of confusing, so I'm going to ask here for what I hope will be a clearer answer.
I have an assignment to use the Comparable interface to sort objects in an array by customer name. I have only done this with integers so far, so I'm not sure how to compare the strings together. How would I go about that? Here is where I am so far, assuming I am to use a.name compared to this.name:
public int comparedTo(Customer a)
{
} //end comparedTo
I also need to make a class to implement the Comparator interface to sort the values based on customer purchases and I think I did that properly, but I'd like to make sure before I go ripping my hair out when it's wrong. Here is what I did for that:
class NameComparator implements Comparator{
public int compare(Object cust1, Object cust2){
String cust1Purch = ((Customer)cust1).purchase;
String cust2Purch = ((Customer)cust2).purchase;
return cust1Purch.compareTo(cust2Purch);
}
Any help is greatly appreciated!