JAVA8 Stream之Sort排序comparing和thenComparing

要先对amount进行升序排序,再针对amount相同的 根据id升序排序

List.sort(Comparator.comparing(Entity::getAmount).thenComparing(Entity::getId));

List.stream().sorted(Comparator.comparing(Entity::getAmount).thenComparing(Entity::getId)).collect(Collectors.toList());

要先对amount进行降序排序,再针对amount相同的 根据id升序排序

List.sort(Comparator.comparing(Entity::getAmount).reversed().thenComparing(Entity::getId));

List.stream().sorted(Comparator.comparing(Entity::getAmount).reversed().thenComparing(Entity::getId)).collect(Collectors.toList());

猜你喜欢

转载自blog.csdn.net/automal/article/details/113954926