Java Stream How to - Sort using the static method from Comparator.comparing()








Question

We would like to know how to sort using the static method from Comparator.comparing().

Answer

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
//from   w w  w. ja v a2 s .co  m
public class Main {

    public static void main(String[] args) {
        List<Person> people = new ArrayList<Person>();
        people.add(new Person("C", 21));
        people.add(new Person("T", 20));
        people.add(new Person("B", 35));
        people.add(new Person("A", 22));
        people.sort(Comparator.comparing(Person::getName));
        people.forEach(System.out::println);
    }

}
class Person {
    private String name;
    private int age;
        
    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return this.name;
    }

    public int getAge() {
        return this.age;
    }
    
    @Override
    public String toString(){
        return this.name + " (" + this.age + ")";
    }

}

The code above generates the following result.