Im trying to implement a comparator for a custom class so I can choose which field to sort by. one of the comparator classes I made is giving me an error I do not understand in the slightest. feel free to point out anything you see, beacause my boss left early on friday and I didn’t get to ask him questions once I started trying to sort an array of the “Spectrum” objects in the code below:
import java.io.*;
import java.util.*;
public class scanComparator<Spectrum> implements Comparator<Spectrum> {
public int compare(Spectrum o1, Spectrum o2){
int s1 = o1.getScanNumber();
int s2 = o2.getScanNumber();
int eval = s2 - s1;
return eval;
}
}
this is the error (from jGrasp):
scanComparator.java:7: error: cannot find symbol
int s2 = o2.getScanNumber();
^
symbol: method getScanNumber()
location: variable o2 of type Spectrum
where Spectrum is a type-variable:
Spectrum extends Object declared in class scanComparator
getScanNumber() is public and not miss-spelled. I re copy-pasted the method name from the Spectrum class. the same error is given for o1 but ommitted because its redundant.
Your problem is you are declaring
scanComparator<Spectrum>, just change it to ‘scanComparator’ as follow and everything goes fine.