stream流多字段排序
时间: 2023-09-10 13:14:08 浏览: 97
对于多字段排序,你可以使用Java的Stream API来处理。下面是一个示例代码,演示了如何使用Stream流进行多字段排序:
```java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 25));
people.add(new Person("Bob", 30));
people.add(new Person("Charlie", 20));
// 按姓名升序,年龄降序排序
Comparator<Person> nameComparator = Comparator.comparing(Person::getName);
Comparator<Person> ageComparator = Comparator.comparing(Person::getAge).reversed();
List<Person>
阅读全文