0

I wrote the following line of code for a Java 8 project:

Comparator<Person> oldestPerson= Comparator.comparing(Person::getOldestBirthday);

It is a simple comparator, however I am trying to use this code In a project that only uses Java 7, therefore giving this error:

"Method references are not supported at this language level"

I understand im getting the error as im not using Java 8. Is there a simple way to re-write the logic without having to use a method reference?

java123999
  • 6,974
  • 36
  • 77
  • 121

1 Answers1

4
Comparator<Person> comparator = new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.getOldestBirthday().compareTo(p2.getOldestBirthday());
    }
};

This is probably the simplest way to do it in Java 7

Leo G.
  • 458
  • 2
  • 10
  • ok so what is there are multiple persons that need to be compared? Rather than just 2 (i.e. p1 and p2)? – java123999 Aug 19 '16 at 13:39
  • Comparators basically define how objects of a same type should be ordered. if you want to apply it to a collection of persons eg. List personList, then you can order them using the comparator : Collections.sort(personList, comparator). – Leo G. Aug 19 '16 at 13:53
  • I mean is there a way to sort on multiple clauses i.e. similar to the .thenComparing() method in Java 8? – java123999 Aug 22 '16 at 08:38