首页javastream_sortJava Stream - 如何使用Comparator.comparing()中的静态方法进行排序

Java Stream - 如何使用Comparator.comparing()中的静态方法进行排序

我们想知道如何使用Comparator.comparing()中的静态方法进行排序。
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

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 + ")";
    }

}