views:

235

answers:

3

I'm getting a compiler error for this line:

Collections.sort(terms, new QuerySorter_TFmaxIDF(myInteger));

My customized Comparator is pretty basic; here's the signature and constructor:

public class QuerySorter_TFmaxIDF implements Comparator<Term>{  
private int numberOfDocs;
QuerySorter_TFmaxIDF(int n){
    super();
    numberOfDocs = n;
}

}

Is there an error because I'm passing an argument into the Comparator? I need to pass an argument...

+2  A: 

There's no reason you can't pass an argument to that constructor. Your code is missing:

  1. The superclass. Your constructor calls super() so I assume there is one; and

  2. The compare() method required by the Comparator interface.

What exactly is numberOfDocs meant to do here?

cletus
@cletus compare() is implemented, but it is too large to post in this comment. you're right, i don't need the super() call, but i'm still getting the same error without that line. numberOfDocs is used in the compare() method for a calculation@oedo the error is:cannot find symbolmethod sort (Java.util.ArrayList<java.Lang.String>, projectPackage.QuerySorter_TFmaxIDF)
+1  A: 

Your Comparator needs to compare Strings because your ArrayList contains Strings.

public class QuerySorter_TFmaxIDF implements Comparator<Term> {  

has to be

public class QuerySorter_TFmaxIDF implements Comparator<String> {  
Skip Head
A: 

The problem lies with your comparator. It's for sorting Term-s but the array you're handing it via the Collections.sort() method has elements of type String.

Steve Emmerson