0

I have a method in the class EarthquakeDataSet called public void mergeSort() where I create an object and try to call a sort() method from my MergeSorter class. The MergeSorter class itself is not my code, it's written by someone else but I'm supposed to call the sort() and use this class that someone else has written. The exact error I'm getting is: "The method sort(E[],Comparator ) in the type MergeSorter is not applicable for arguments"

My method in EarthquakeDataSet looks like this:

public void mergeSort(){
MergeSorter obj = new MergeSorter();
obj.sort();

}

The method in MergeSorter I'm trying to call, calls another method, which also calls another method. This is my MergeSorter class.

The three methods in MergeSorter looks like this:

public static <E> void sort(E[] a, Comparator<? super E> comp) {
     mergeSort(a, 0, a.length - 1, comp); //calling mergeSort method
   }

and

private static <E> void mergeSort(E[] a, int from, int to, Comparator<? super E> comp){
}

and

private static <E> void merge(E[] a, int from, int mid, int to, Comparator<? super E> comp) {

}

There's a fair bit of code inside but I'm just having trouble calling with these arguments.

4 Answers4

2

As shown by both the error message and the method code you've posted, the sort method takes two arguments - firstly an array of things to sort, and secondly a Comparator over those things to define the ordering.

You're trying to call sort() with no arguments, which isn't legal and won't compile. (And even if it did compile - what would you expect this to do? What exactly would it be sorting?)

You'll possibly need to update your own mergeSort method to take the data to be sorted as a parameter. This might not be necessary if it's a field in the class - but either way, you'll need to pass that data into MergeSorter.sort.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • I have a a lot of records and I'm using different sort methods to sort said records. I'm still unsure of what arguments to use with the sort method though, I've never used a Comparator as an argument before. – Craig Anderson Oct 09 '13 at 15:43
  • Well, the first argument is going to be the array of records that you want to sort! [Comparator](http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html) is an interface which defines an ordering over a certain class. It does this by implementing a function that takes two elements and says whether the first is "less than", "equal to" or "greater than" the second. If the records to be sorted are standard `ints` or `Strings` then likely a Comparator already exists, else you might need to write your own (it's just one short method). – Andrzej Doyle Oct 09 '13 at 15:48
  • It's starting to make more sense based off of what you said. The thing is I'm sorting in multiple ways. I have these records of Earthquakes that can be sorted by city, magnitude, and depth all of which I have variables for from an "EarthquakeRecord[ ]" array, but how would I use the comparator as a 2nd argument? – Craig Anderson Oct 09 '13 at 16:02
  • It depends how you *want* to sort them, on that particular call. If you want to sort by magnitude, pass a Comparator whose implementation is something like `return o1.magnitude - o2.magnitude;`. If you want to sort by city, use a Comparator that works something like `return o1.city.compareTo(o2.city);`. (You'd probably need to handle nulls, and tie-breakers if the magnitudes were equal, but this is the gist of it.) This is **why** the method takes a Comparator, so that the caller can decide what they want the sorting to be based on. – Andrzej Doyle Oct 09 '13 at 16:20
0

You're calling

obj.sort();

But the method signature looks like this:

public static <E> void sort(E[] a, Comparator<? super E> comp)

The error you're getting is telling you you're using the wrong arguments, or in this case, you're not using any arguments at all.

An example of correct usage would be something like:

String[] values = "Hello my name is chris".split(" ");
obj.sort(values, new Comparator<String>() {
    public int compare(String s1, String s2)
    {
         return s1.compareTo(s2);
    }
);
christopher
  • 26,815
  • 5
  • 55
  • 89
0

First of all, sort method of MergeSorter is static, so no need to create object to call the method, and then you are calling, obj.sort(), which clearly does not pass any argument to the method whose signature is

public static <E> void sort(E[] a, Comparator<? super E> comp)

you must pass two arguments to the method, as in example

    class Try<E> implements Comparator<E>
   {
      public int compare(E o1, E o2) {
     //logic here
     }

  }

public class Test {

    static Try<String>[] a = new Try[5];

    public static void main(String[] args) {
        sort(a, new Try());
    }

    public static <E> void sort(E[] a, Comparator<? super E> comp)
    {
        //sort logic
    }

  }

I am calling from main method, in you case you can call like MergeSorter.sort(a, new Try());

Hope I am clear.

codingenious
  • 8,385
  • 12
  • 60
  • 90
0

You referred to the things you're sorting as "records". Usually, when sorting records, the records have several data items but you want to sort on one particular item (e.g. in alphabetical order by city name); or sometimes multiple items (e.g. alpha order by city name, and then for records in the same city, in ascending order by time or something). A comparator would look something like this:

EarthquakeRecord[] values = <whatever>;
obj.sort (values, new Comparator<EarthquakeRecord>() {
    public int compare (EarthquakeRecord r1, EarthquakeRecord r2) {
         return r1.getCity().compareTo (r2.getCity());
    });

Or to sort on multiple items:

obj.sort (values, new Comparator<EarthquakeRecord>() {
    public int compare (EarthquakeRecord r1, EarthquakeRecord r2) {
         int cityCompare = r1.getCity().compareTo (r2.getCity());
         if (cityCompare != 0)
             return cityCompare;
         return Integer.compare (r1.getTime(), r2.getTime());
             // assuming getTime() returns the time as an integer, somehow
    });

Or to sort in descending order:

obj.sort (values, new Comparator<EarthquakeRecord>() {
    public int compare (EarthquakeRecord r1, EarthquakeRecord r2) {
         return Double.compare (r2.getMagnitude(), r1.getMagnitude());
         // this will also work:
         // return -Double.compare (r1.getMagnitude(), r2.getMagnitude());
    });

The compare and compareTo methods return a negative integer if the first argument is less, a positive integer if the first argument is larger, 0 if they're equal. That's all that's required for sort.

ajb
  • 31,309
  • 3
  • 58
  • 84