How to sort a List on objects in Java using a Compator
In this blog post, I will be explaining how you can sort a List of objects in Java using the Comparator interface. Input List Suppose, there is a Person class as follows: public class Person {private String firstName;private String lastName;private int age;public Person(String firstName,String lastName,int age){this.firstName=firstName;this.lastName=lastName;this.age=age;//getter and setter methods public String toString(){ return firstName+" "+lastName+":"+age; }}} And you have a list of Person objects as follows: Person person1 = new Person("Mickey", "Mouse",34); Person person2 = new Person("Donald", "Duck",45); Person person3 = new Person("Peppa", "Pig",12); List<Person> people = new ArrayList<Person>(); people.add(person1); people.add(person2); people.add(person3); Using Comparator to sort in increasing order of age And suppose you want to sort them in the increasing order of a...