If you do not have a restriction to only use sort, I have a contrived way to do an in place reversal using lambdas:
import java.util.Arrays;
import java.util.Collections;
import java.util.stream.IntStream;
public class ReverseArr {
public static void main(String[] args) {
Integer[] arr = { 4, 5, 61, 3, 9, 3, 1, -4, 7, 2, -8, 6, -3 };
IntStream.rangeClosed(0, arr.length - 1)
.boxed()
.sorted(Collections.reverseOrder())
.forEach(e -> {
if ((arr.length - e) - e > 1) {
int temp = arr[e];
arr[e] = arr[arr.length - e - 1];
arr[arr.length - e - 1] = temp;
}
});
Arrays.stream(arr)
.forEach(e -> System.out.print(e+ " "));
}
}
As the comment mentions, we have to do the reversal based on the indices. So I just generated those values and switch the values of arr using those indices through lambdas.
A key part is this condition: (arr.length - e) - e > 1. What it means is to stop reversing the elements once we get to the middle (the middle is when the difference is 1 if the array has an even number of elements and it is 0 if the array has an odd number of elements. This way we do not undo our reversal.
Output:
-3 6 -8 2 7 -4 1 3 9 3 61 5 4
sort(as it depends on the indices, not just the values)