Search code examples
javacomparatorcomparable

Comparator as a parameter for the constructor of a class


I can't seem to find exactly how to do this anywhere. I am writing a class that takes a comparator as a parameter/argument for the constructor of a class. I want to use that to order items in a list. But I'm not sure what to do with the comparator in the new class.

  • I have imported java.util.Comparator
  • In the class declaration I said "impements Comparator<T>"
  • I have written (Comparator<T> c) as the argument for the constructor of the class.

I have never used a comparator this way--I've used it as an inner class and that is it so I'm not sure how to make the compare method work in this class when I take the comparator in as an argument to the constructor.

The only things I've done that are the three bulleted items above. Anytime I try to do something with comparator I am taking in as an argument, I get an error message.

Here is the code for the constructor:

public class SortedList<T> implements Comparator<T>
    //value, position and array are instance variables
    //I am switching array to a List to deal with generics
    private int position;
    private Integer[] array;

    public SortedList(Comparator<T> c){
       this.position = 0;
       this.array = new Integer[25];
    }

    public void sort(Integer num){
       boolean valid = false;
       int i = 0;

       while(!valid && i < array.length-1){
          if(num.compareTo(array[i] > 0)){
            array[i+1] = array[i];
            array[i] = num;
          }else{
            i++;
       }
    }

The error messages I have been getting are:

  • Cannot find symbol - method compareTo

I would like to be able to compare any two objects, not just integers, that was why I wanted to take a comparator as a parameter.


Solution

  • Your SortedList<T> class must not implement the Comparator<T> interface, because this class is not used for comparing objects. However, it will use a given Comparator<T> instance to sort its entries. This means the classes and methods should have the following definitions:

    public class SortedList<T> {
         // ...
    }
    

    The class does not implement the Comparator<T> interface anymore.

    private T[] array;
    

    The array field should be of type T[] since this SortedList<T> object is used to sort/hold objects of type T, not Integer objects.

    public SortedList(Comparator<T> c){
        // ...
        this.comparator = c;
    }
    

    That's correct. The constructor receives a Comparator<T> instance. You should store this reference to a field so you can later use it in your sort() method.

    public void sort(){
        // ...
    }
    

    The Integer argument on the sort() method doesn't make any sense, so delete it. Now you can use the stored Comparator<T> instance in your sort() method and call its compare() method to compare two objects from your stored array. A code fragment might look like this:

    // ...
    if (this.comparator.compare(this.array[i], this.array[i+1])) {
        // it should be on the left
    } else {
        // it should be on the right
    }
    // ...