I have the following List:
List<String> fruits = new ArrayList<String>(Arrays.asList("Apple", "Banana", "Orange", "Watermelon", "Peach"));
I need to order it with this condition:
The result should be:
Orange, Watermelon, Apple, Banana, Peach
How can I do it in Java 8?
You could use a Comparator chain.
List<String> fruits = new ArrayList<>(Arrays.asList(
"Apple", "Banana", "Orange", "Watermelon", "Peach"));
fruits.sort(Comparator.comparing((String s) -> !s.contains("O") && !s.contains("o"))
.thenComparing(Comparator.naturalOrder()));
System.out.println(fruits.stream().collect(Collectors.joining(", ")));
The first comparator looks at whether the string contains an O making the ones which do go first, and the second comparator uses natural ordering.
Note: Boolean.TRUE.compareTo(Boolean.FALSE) > 0
so to make the strings which contain an O go first I flip this with a !
This prints
Orange, Watermelon, Apple, Banana, Peach
NOTE: In this case, the new ArrayList<>( ... )
isn't needed as sort
doesn't change the number of elements, just re-arranges them.
EDIT: Based on @Holger's suggestion.
If a stable sort is preferred, i.e. leave the order as it was at much as possible.
List<String> fruits = Arrays.asList(
"Watermelon", "Peach", "Orange", "Banana", "Apple");
fruits.sort(Comparator.comparing((String s) -> !s.contains("O") && !s.contains("o")));
System.out.println(fruits.stream().collect(Collectors.joining(", ")));
prints
Watermelon, Orange, Peach, Banana, Apple
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With