Only posting this here because I have absolutely no idea how to google this. Consider the following code. Below, we use people.sort(Comparator.comparingInt(Person::getAge));
to sort through the people so that their ages are stored from least to greatest.
How can we sort from greatest to least using Comparator and a method reference operator?
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class Main {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Sachin", 47));
people.add(new Person("Chris", 34));
people.add(new Person("Rajeev", 25));
people.add(new Person("David", 31));
System.out.println("Person List : " + people);
// Sort People by their Age
//people.sort((person1, person2) -> {
// return person2.getAge() - person1.getAge();
});
// A more concise way of writing the above sorting function
people.sort(Comparator.comparingInt(Person::getAge));
System.out.println("Sorted Person List by Age : " + people);
// You can also sort using Collections.sort() method by passing the custom Comparator
Collections.sort(people, Comparator.comparing(Person::getName));
System.out.println("Sorted Person List by Name : " + people);
}
}