Lish
Lish

Reputation: 307

Using the Comparable interface when comparing Strings

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!

Upvotes: 2

Views: 18917

Answers (5)

aioobe
aioobe

Reputation: 421090

Here is a complete example that might help you:

A CustomerComparator:

class CustomerComparator implements Comparator<Customer> {

    @Override
    public int compare(Customer c1, Customer c2) {
        return c1.name.compareTo(c2.name);   // or, simply c1.compareTo(c2);
    }
}

A Comparable Customer:

class Customer implements Comparable<Customer> {

    String name;

    public Customer(String name) {
        this.name = name;
    }

    @Override
    public int compareTo(Customer o) {
        return name.compareTo(o.name);
    }

    public String toString() {
        return name;
    }
}

A simple test driver:

public class Test {
    public static void main(String[] args) {

        List<Customer> customers = Arrays.asList(new Customer("Bravo"),
                                                 new Customer("Charlie"),
                                                 new Customer("Delta"),
                                                 new Customer("Alpha"));
        Collections.sort(customers);

        // Or
        // Collections.sort(customers, new CustomerComparator());

        System.out.println(customers);

    }
}

(ideone.com demo)

Upvotes: 3

Andrey Adamovich
Andrey Adamovich

Reputation: 20663

1) I would use generics to define your comparator and avoid additinal class casting:

class NameComparator implements Comparator<Customer> {
    public int compare(Customer cust1, Customer cust2) {
      ...
    }
}

2) String class in java already implements Comparable interface ( http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html ). So, if you need to just compare on customer's name or purchase string, then you can just delegate it to String and that's what you already do.

Upvotes: 0

Yanick Rochon
Yanick Rochon

Reputation: 53576

I seem to get it right for the Comparable interface. Nothing really complicated there.

As for the Comparator, if you're not using generics, you also need to validate both argument for the same base type, at least Comparable since you're using that interface :

if (cust1 instanceof Comparable && cust2 instanceof Comparable) {
   Comparable c1 = (Comparable) cust1;
   Comparable c2 = (Comparable) cust2;
   return c1.compareTo(c2);
} else {
   return false;
}

Upvotes: 0

Sergey Vedernikov
Sergey Vedernikov

Reputation: 7754

Its all ok, but you can specify Comparator generic type and then no need to cast objects:

class NameComparator implements Comparator<Customer>{
public int compare(Customer cust1, Customer cust2){    

    String cust1Purch = cust1.purchase;        
    String cust2Purch = cust2.purchase;

    return cust1Purch.compareTo(cust2Purch);
}

Upvotes: 3

Bozho
Bozho

Reputation: 597224

Looks fine. But you can utilize Generics:

class NameComparator implements Comparator<Customer> {
    public int compare(Customer cust1, Customer cust2) {..}
}

Upvotes: 0

Related Questions