Reputation: 342
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
sortmethod.
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
Upvotes: 0
Views: 501
Reputation: 912
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:
actors.sort(new AgeComparator());
, Collections.sort
is too commonUpvotes: 2