I am new to Java and this might be a dumb question to ask. I was looking at '905. Sort Array By Parity' on Leetcode. One of the solutions is
class Solution {
public int[] sortArrayByParity(int[] A) {
Integer[] B = new Integer[A.length];
for (int t = 0; t < A.length; ++t)
B[t] = A[t];
Arrays.sort(B, (a, b) -> Integer.compare(a%2, b%2));
for (int t = 0; t < A.length; ++t)
A[t] = B[t];
return A;
/* Alternative:
return Arrays.stream(A)
.boxed()
.sorted((a, b) -> Integer.compare(a%2, b%2))
.mapToInt(i -> i)
.toArray();
*/
}
}
I am having trouble understanding the line with lambda expression in it.
Arrays.sort(B, (a, b) -> Integer.compare(a%2, b%2));
How does it sort the array exactly? Where do a and b come from?