Say I have a class named Person
.
class Person(){
private int age;
}
And I have a List of Persons with ages 7, 2, 9, 4 and 17. I want to sort the list of Person objects in ascending order by the absolute difference of the Person's age with a given value.
For example, I want to sort the Persons based on the absolute difference of the Person's age and a given age say 7.
The result would be a List of Persons Objects with ages 7, 9, 4, 2, 17
.
This is because
abs(7 - 7) = 0
abs(7 - 9) = 2
abs(7 - 4) = 3
abs(7 - 2) = 5
abs(7 - 17) = 10
As you can see I want to sort the list of Persons Objects based on the absolute difference of a given value (this value is not always the same).