Java 8 How to sort list with stream.sorted()
By:Roy.LiuLast updated:2019-08-11
Few examples to show you how to sort a List with stream.sorted()
1. List
1.1 Sort a List with Comparator.naturalOrder()
package com.mkyong.sorted; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StreamApplication { public static void main(String[] args) { List<String> list = Arrays.asList("9", "A", "Z", "1", "B", "Y", "4", "a", "c"); /* List<String> sortedList = list.stream() .sorted(Comparator.naturalOrder()) .collect(Collectors.toList()); List<String> sortedList = list.stream() .sorted((o1,o2)-> o1.compareTo(o2)) .collect(Collectors.toList()); */ List<String> sortedList = list.stream().sorted().collect(Collectors.toList()); sortedList.forEach(System.out::println);
Output
1.2 Sort a List with Comparator.reverseOrder()
package com.mkyong.sorted; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class StreamApplication { public static void main(String[] args) { List<String> list = Arrays.asList("9", "A", "Z", "1", "B", "Y", "4", "a", "c"); /* List<String> sortedList = list.stream() .sorted((o1,o2)-> o2.compareTo(o1)) .collect(Collectors.toList()); */ List<String> sortedList = list.stream() .sorted(Comparator.reverseOrder()) .collect(Collectors.toList()); sortedList.forEach(System.out::println);
Output
2. List Objects
1.1 Sort by age, natural order.
package com.mkyong.sorted; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; public class StreamApplication { static List<User> users = Arrays.asList( new User("C", 30), new User("D", 40), new User("A", 10), new User("B", 20), new User("E", 50)); public static void main(String[] args) { /*List<User> sortedList = users.stream() .sorted((o1, o2) -> o1.getAge() - o2.getAge()) .collect(Collectors.toList());*/ List<User> sortedList = users.stream() .sorted(Comparator.comparingInt(User::getAge)) .collect(Collectors.toList()); sortedList.forEach(System.out::println); static class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; public String getName() { return name; public void setName(String name) { this.name = name; public int getAge() { return age; public void setAge(int age) { this.age = age; @Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}';
Output
User{name='A', age=10} User{name='B', age=20} User{name='C', age=30} User{name='D', age=40} User{name='E', age=50}
1.2 reverse order.
List<User> sortedList = users.stream() .sorted(Comparator.comparingInt(User::getAge) .reversed()) .collect(Collectors.toList()); sortedList.forEach(System.out::println);
Output
User{name='E', age=50} User{name='D', age=40} User{name='C', age=30} User{name='B', age=20} User{name='A', age=10}
1.3 Sort by name
/*List<User> sortedList = users.stream() .sorted((o1, o2) -> o1.getName().compareTo(o2.getName())) .collect(Collectors.toList());*/ List<User> sortedList = users.stream() .sorted(Comparator.comparing(User::getName)) .collect(Collectors.toList());
From:一号门
COMMENTS