0

my goal is to sort the List of Persons. I am using compare method of Comparator interface.

I have created three classes. namely

Person.java, AgeComparator.java, SortingExample.java
.

Person class is a simple class with two fields (name, age) also with getters. Then in my AgeComaparator class I have implement the Comparator interface and pass Person class as Type parameter.

then override the compare method which comes from Comparator interface.

so to execute this application, in my SortingExample I have created 3 objects of type Person. then add those objects to ArrayList of type Person.

then called the Collections.sort() method which take two arguments. those are list object and the class which implements Comparator interface.

Person.java

package com.nadee.module3;

import java.util.Objects;

public class Person {

    private final String name;
    private final int age;

    public Person(String name, int age) {
        Objects.requireNonNull(name);
        this.name = name;
        this.age = age;
    }


    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }


    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}

AgeComparator.java

package com.nadee.module3;

import java.util.Comparator;

public class AgeComparator implements Comparator<Person>{

    @Override
    public int compare(Person left, Person right) {
        return Integer.compare(left.getAge(), right.getAge());
    }

}

SortingExample.java

package com.nadee.module3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.nadee.collections.Person;

public class SortingExample {
    public static void main(String[] args) {


    Person p1 = new Person("will smith", 45);
    Person p2 = new Person("john snow", 33);
    Person p3 = new Person("johny depth", 55);

    List<Person> actors = new ArrayList<Person>();

    actors.add(p1);
    actors.add(p2);
    actors.add(p3);

    System.out.println(actors);

    Collections.sort(actors, new AgeComparator());

    System.out.println(actors);

    }
}

I'm using Eclipse IDE for this application and there is "red" line showing under the

sort 
method.

the error is saying like following,

"The method sort(List, Comparator) in the type Collections is not applicable for the arguments (List, AgeComparator)"

I have followed the following article https://www.journaldev.com/16094/java-collections-sort

what I'm doing wrong here ? any help would be much appreciated.

thanks

Nadee
  • 342
  • 1
  • 6
  • 18
  • 1
    You import `import com.nadee.collections.Person` but the Person class you showed us is located in a different package - `com.nadee.module3`. It that a typo? – Eran Oct 17 '19 at 05:40
  • This code works fine when all 3 classes are in the same package. – Eran Oct 17 '19 at 05:41
  • in other words, the `Agecomparator` is for one class `...collections3.Person`, while the list contains a different one `...collection.Person` – user85421 Oct 17 '19 at 05:44
  • As a note, you can just use `Comparator.comparing(Person::getAge)`. – daniu Oct 17 '19 at 05:44
  • @Eran, yes , I was importing from wrong package. – Nadee Oct 17 '19 at 06:31

1 Answers1

2

Class Person is located at com.nadee.module3 and class AgeComparator is using for this kind of Person

But in class SortingExample, you're using Person from com.nadee.collections.Person. It should totally not work. Another point of your code:

  • Please using actors.sort(new AgeComparator());, Collections.sort is too common
Tea
  • 873
  • 2
  • 7
  • 25
  • silly of me. thanks @Tea NG, it was the importing issue, i was able to fix it. – Nadee Oct 17 '19 at 06:29
  • 1
    What does "`Collections.sort` is too common" mean? You are right that you may as well invoke `List.sort` directly, given that `Collections.sort` delegates to that; I just don't understand what you mean by that statement. – Andy Turner Oct 17 '19 at 06:34
  • @AndyTurner. Currently, I'm using Java 8. And if you also using Java 8, you'll not see the difference between Collections.sort (since jdk1.2) and List.sort (since jdk8). In Java older version Collections.sort is a bit different Checkout link to see Java 7. https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/Collections.java#L215 You might read this info to see more: https://stackoverflow.com/a/34827492/2865414 – Tea Oct 17 '19 at 08:23
  • 1
    @TeaNG I am querying the words "too common". Why does it matter if something is "common" (is this in the sense of "general", or "frequently-used"?), and what makes it overly so? – Andy Turner Oct 17 '19 at 08:31
  • In my point of view, even it's just a piece of code " Everything should be clear if can". Someone look and use my code, they can feel free to do what they want without any concern. It has no reason to suggest you should use something you don't think it's necessary, dude. – Tea Oct 17 '19 at 08:41