2

I was trying to implement compare method for my "WordData" objects. I would like to compare two String fields of object which are "word" Plese see below. This is my compare method implementation:

 Arrays.sort(list, new Comparator<WordData>(){

        @Override
        public int compare(WordData o1, WordData o2){
            return o1.getWord().compareTo(o2.getWord());
        }

    });

And this is my WordData class:

public class WordData {

private String word;
private int counter =0;
private String[] positions = new String[1];

public WordData(String word, int position, int lineNo, int counter) {
    this.word=word;
    this.counter=counter;
    positions[0]=String.valueOf(position)+", "+String.valueOf(lineNo);
}


public String getWord() {
    return word;
}}

I got error message in my compare() method which is like:

Error:(62, 15) java: no suitable method found for sort(java.util.List<PrintTest.WordData>,<anonymous java.util.Comparator<PrintTest.WordData>>)
    method java.util.Arrays.<T>sort(T[],java.util.Comparator<? super T>) is not applicable
      (cannot infer type-variable(s) T
        (argument mismatch; java.util.List<PrintTest.WordData> cannot be converted to T[]))
    method java.util.Arrays.<T>sort(T[],int,int,java.util.Comparator<? super T>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))
mastach
  • 23
  • 5

0 Answers0