import java.util.List; import java.util.ArrayList; public class SortTest1 { public static void main(String[] args) { List<Person> people = new ArrayList<>(); people.add(new Person("Alice", 30)); people.add(new Person("Bob", 19)); people.add(new Person("Ronald", 40)); people.add(new Person("Jimi", 27)); people.add(new Person("Suzan", 60)); people.add(new Person("Jeff", 80)); people.add(new Person("Maria", 52)); // sort by name people.sort((p1, p2)->p1.getName().compareTo(p2.getName())); System.out.println(people); // [name:Alice age:30, name:Bob age:19, name:Jeff age:80, name:Jimi age:27, name:Maria age:52, name:Ronald age:40, name:Suzan age:60] // sort by age people.sort((p1, p2)->p1.getAge().compareTo(p2.getAge())); System.out.println(people); // [name:Bob age:19, name:Jimi age:27, name:Alice age:30, name:Ronald age:40, name:Maria age:52, name:Suzan age:60, name:Jeff age:80] } } class Person { private String name; private Integer age; public Person(String name, Integer age) { this.name = name; this.age = age; } public Integer getAge() { return age; } public String getName() { return name; } public String toString() { return "name:" + name + " age:" + age; }