Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The method compare(long, long) is undefined for the type Long

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.

like image 421
Vlad Alexeev Avatar asked Nov 23 '13 13:11

Vlad Alexeev


2 Answers

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.

like image 79
JB Nizet Avatar answered Nov 03 '22 00:11

JB Nizet


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.*.

like image 37
eric gilbertson Avatar answered Nov 03 '22 00:11

eric gilbertson