Reputation: 486
I was looking around for a method to sort a list of objects based on just one of its multiple fields (and i ended up just asking the question myself) but in my research, I came across this answer:
https://stackoverflow.com/a/1421537/1549672
I'm fairly new to java and this may be why, but I don't quite understand this last method:
public static Comparator<Person> getComparator(final PersonComparator... multipleOptions) {
return new Comparator<Person>() {
public int compare(Person o1, Person o2) {
for (PersonComparator option : multipleOptions) {
int result = option.compare(o1, o2);
if (result != 0) {
return result;
}
}
return 0;
}
};
}
could someone please explain how it works...and what exactly it does? Thanks!
Upvotes: 3
Views: 2652
Reputation: 124235
getComparator
method will return comparator that will compare two Person
objects based on comparators passed in its arguments. Each of PersonComparator
s is designed to compare Person by one of Person class field, for example
PersonComparator.ID_SORT
will compare Person objects by id
(0 < 1 < 2 < 3 < ...) PersonComparator.NAME_SORT
will compare Person objects by name using natural (dictionary) order ("a" < "aa" < "ab" < "b"). If your Person class have more fields then you can add new comparator to enum PersonComparator
.
Also order of PersonComparators passed to getComparator
method is important.
For example, if you have Persons
Person p1 = new Person(1,"Jack");
Person p2 = new Person(2,"Jack");
Person p3 = new Person(2,"Adam");
and you will create comparator by
getComparator(PersonComparator.ID_SORT,PersonComparator.NAME_SORT)
it will sort person first by their id
s and in case ids ware equal then it will sort them by their name
(1,"Jack") (2,"Adam") (2,"Jack")
Also comparator created by
getComparator(PersonComparator.NAME_SORT, PersonComparator.ID_SORT)
will firs compare names and only if names are equal it will compare ids of Persons, so you will get persons sorted this way
(2,"Adam") (1,"Jack") (2,"Jack")
Upvotes: 1
Reputation: 3497
It will find the first comparator that will not return "equals" and return its value, otherwise it will return "equals".
Actually it will create a new Comparator to do this, by creating an anonymous class based on the Comparator-interface.
public static Comparator<Person> getComparator(
final PersonComparator... multipleOptions)
{
return new Comparator<Person>() { //create anonymous class
public int compare(Person o1, Person o2) { //which implements the only method
//in Comparator
for (PersonComparator option : multipleOptions) { //loop through given
//comparators
int result = option.compare(o1, o2);
if (result != 0) { //return value of the first
return result; //which says "non-equal"
}
}
return 0;//all said "equals" so we say "equals"
}
};
}
Upvotes: 4
Reputation: 10789
You pass list of other comparators final PersonComparator... multipleOptions
to method getComparator
.
Your comparator use this PersonComparators in for loop and returns result if persons not equal due to current PersonComparator.
So, basically, this method returns Comparator, that return 0 if Persons are equal due to all passed comparators or return first non-matching int from the passed comparator compareTo
method.
Upvotes: 0
Reputation: 1110
Basically this method receives a variable number of PersonComparator objects and then iterates through them to compare two person objects returning the first comparator that does not return a match between two given person objects.
Upvotes: 0
Reputation: 66637
You are comparing two person objects using Comparator.
Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second
Upvotes: 1