I do have list like this:
List<Integer> list = new ArrayList<>();
list.add(24);
list.add(12);
list.add(0);
list.add(36);
list.add(1);
list.add(99);
I want to sort it ascendig, but 0 has to always be on the last one. Is there any way to simplify it?
List<Integer> collect = list.stream()
.sorted((a,b) -> b.compareTo(a))
.sorted((a, b) -> Integer.valueOf(0).equals(a) ? 1 : -1)
.collect(Collectors.toList());
CodePudding user response:
List<Integer> list = List.of(24, 12, 0, 36, 1, 99);
List<Integer> sorted = list.stream()
.sorted(Comparator.comparingInt(a -> a == 0 ? Integer.MAX_VALUE : a))
.toList();
System.out.println(sorted);
Seems to work; prints [1, 12, 24, 36, 99, 0]
.
The one downside is that it won't do the right thing if literally the maximum integer value (which is 2147483647
) is in your list, in which case it'll sort the 0s amongst them instead of after them. If that is a problem, nothing is going to look significantly shorter than what you did.
NB: Your code appears to sort descending. In which case the 0 would already sort at the end unless you have negative numbers. If that's what you want and 'ascending' was a typo, you'd have to use MIN_VALUE instead, and reverse the comparator (tack .reverse()
t the end).
CodePudding user response:
You can process in the same sorted
:
List<Integer> collect = list.stream()
.sorted((o1, o2) -> o1 == 0 ? 1 : (o2 == 0 ? -1 : o1.compareTo(o2)))
.collect(Collectors.toList());
CodePudding user response:
May be introducing variables for the comparators and comparing the boolean if an int equals to zero might simplify or at least make it readable
Comparator<Integer> natural = Comparator.naturalOrder();
Comparator<Integer> zeroLast = Comparator.comparing(i -> i.equals(0));
List<Integer> collect = list.stream()
.sorted(natural)
.sorted(zeroLast)
.collect(Collectors.toList());
CodePudding user response:
You can use 1 ternary operator in the same sort function. Check the following
List<Integer> collect = list.stream()
.sorted((o1, o2) -> (o1 == 0 || o2 == 0) ? -Integer.compare(Math.abs(o1), Math.abs(o2)) : o1.compareTo(o2))
.collect(Collectors.toList());