Stream流不同数据类型排序

使用sorted方法,需要传入一个Comparator比较器,默认自然排序也就是升序(从小到大)。
如果想要反过来则需要额外传入一个Comparator.reverseOrder()参数,即降序(从大到小)。

public class Test {

    public static void main(String[] args) {
        List<User> list = Arrays.asList(
                new User("小赵", 20, new BigDecimal("123.33"), DateUtil.parseDateTime("2014-5-18 23:12:29")),
                new User("小钱", 9, new BigDecimal("89.12"), DateUtil.parseDateTime("2023-2-18 06:45:12")),
                new User("小孙", 18, new BigDecimal("280.28"), DateUtil.parseDateTime("2009-3-31 16:33:45")),
                new User("小李", 13, new BigDecimal("1000.99"), DateUtil.parseDateTime("1992-12-4 10:48:08"))
        );
        list.forEach(System.out::println);
        System.out.println();

        //Integer
        System.out.println("Integer升序");
        list.stream().sorted(Comparator.comparing(User::getAge)).forEach(System.out::println);
        System.out.println("Integer降序");
        list.stream().sorted(Comparator.comparing(User::getAge, Comparator.reverseOrder())).forEach(System.out::println);
        System.out.println();

        //BigDecimal
        System.out.println("BigDecimal升序");
        list.stream().sorted(Comparator.comparing(User::getMoney)).forEach(System.out::println);
        System.out.println();

        //Date
        System.out.println("Date升序");
        list.stream().sorted(Comparator.comparing(User::getBirthday)).forEach(System.out::println);
        System.out.println();
    }

}

@Data
@ToString
@AllArgsConstructor
class User {

    private String name;

    private Integer age;

    private BigDecimal money;

    private Date birthday;

}
User(name=小赵, age=20, money=123.33, birthday=2014-05-18 23:12:29)
User(name=小钱, age=9, money=89.12, birthday=2023-02-18 06:45:12)
User(name=小孙, age=18, money=280.28, birthday=2009-03-31 16:33:45)
User(name=小李, age=13, money=1000.99, birthday=1992-12-04 10:48:08)

Integer升序
User(name=小钱, age=9, money=89.12, birthday=2023-02-18 06:45:12)
User(name=小李, age=13, money=1000.99, birthday=1992-12-04 10:48:08)
User(name=小孙, age=18, money=280.28, birthday=2009-03-31 16:33:45)
User(name=小赵, age=20, money=123.33, birthday=2014-05-18 23:12:29)
Integer降序
User(name=小赵, age=20, money=123.33, birthday=2014-05-18 23:12:29)
User(name=小孙, age=18, money=280.28, birthday=2009-03-31 16:33:45)
User(name=小李, age=13, money=1000.99, birthday=1992-12-04 10:48:08)
User(name=小钱, age=9, money=89.12, birthday=2023-02-18 06:45:12)

BigDecimal升序
User(name=小钱, age=9, money=89.12, birthday=2023-02-18 06:45:12)
User(name=小赵, age=20, money=123.33, birthday=2014-05-18 23:12:29)
User(name=小孙, age=18, money=280.28, birthday=2009-03-31 16:33:45)
User(name=小李, age=13, money=1000.99, birthday=1992-12-04 10:48:08)

Date升序
User(name=小李, age=13, money=1000.99, birthday=1992-12-04 10:48:08)
User(name=小孙, age=18, money=280.28, birthday=2009-03-31 16:33:45)
User(name=小赵, age=20, money=123.33, birthday=2014-05-18 23:12:29)
User(name=小钱, age=9, money=89.12, birthday=2023-02-18 06:45:12)

觉得内容还不错?打赏个钢镚鼓励鼓励!!👍