Reputation: 1124
So I would like to sort everything out in my ArrayList by using the objects String Name that I have assigned to it.
So far I've read about Comparators to get what I want done. But when I implement it, I get a compile error.
I initialize it like this
private static ArrayList<PeopleInfo> infoArray = new ArrayList<PeopleInfo>();
and call the sort like this
Collections.sort(infoArray, new CustomComparator());
and this is the Class.
public class CustomComparator implements Comparator<PeopleInfo> {
@Override
public int compare(PeopleInfo o1, PeopleInfo o2) {
return o1.GetLast().compareTo(o2.GetLast());
}
}
The error I get is "No enclosing instance of type mainClass is accessible. Must qualify the allocation with an enclosing instance of type mainClass (e.g. x.new A() where x is an instance of MainClass)."
Not really understanding what's happening. Thanks in advance!
Upvotes: 1
Views: 1285
Reputation: 3679
Alternatively you can declare comparator outside main class.
Upvotes: 0
Reputation: 4389
Declare your Comparator class as a static class
public static class CustomComparator implements Comparator<PeopleInfo> {
Upvotes: 3