I need to make a Comparator to sort my List of objects by one of its variables which is of long
type.
public class ParticipantIndexComparator implements Comparator<Integer> {
final List<Participant> participants;
public ParticipantIndexComparator(ArrayList<Integer> numbersToSort) {
participants=new ArrayList<Participant>();
for (int i=0;i<numbersToSort.size();i++)
{ participants.add(i,competition.participant.get(numbersToSort.get(i))); participants.get(i).comparator=numbersToSort.get(i);}
}
@Override
public int compare(Integer i1, Integer i2) {
long l1 = participants.get(i1).kpTime.get(kpSelected);
long l2 = participants.get(i2).kpTime.get(kpSelected);
return Long.compare(l1, l2);
}
}
But return Long.compare(l1, l2);
is invalid - "The method compare(long, long) is undefined for the type Long".
Seems like I'm doing it the wrong way.
This method exists since Java 7. You're probably using a Java 6 or even older version of Java. In these previous versions, you can simply use
Long.valueOf(l1).compareTo(Long.valueOf(l2));
The javadoc is your friend. Read it.
If you experience Integer.compare being undefined after importing a Maven project into Eclipse, check the project properties to insure that it is using a 1.7 JRE. For some reason the import associated a 1.6 JRE with my project even though the POM, and my system default was 1.7.*.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With