I am working on a sorting 2D-Array problem in Java. I want to sort the array based on the second column in each row such as a[i][1]. I am trying to write a lambda expression to put it in Arrays.sort(a, comparator) to avoid writing a whole class.
Here is what I tried:
Arrays.sort(contests, (int[] num1, int[] num2) -> Integer number1 = num1[1];
Integer number2 = num2[1];
return number2.compareTo(number1);
);
I am not sure what is wrong because Java will not give me an exact error message when I run. I know that the first part of a lambda expression is the input, and the second part is comparing. Since .compareTo() only works for objects, that is why I create two Integers. Eclipse keeps telling me to insert; after Integer. Can anyone help me, please? Thank you
The type definitions inside the lambda expression arise from the context, no further type-definitions are needed.
Your example is only working with the values from array contests[1]
.
Arrays.sort()
expects an 1-dimensional array.
If contests
is defined as Integer[][] array
, then this expression should work for You:
Arrays.sort( contests[1], (num1, num2) -> ( num2.compareTo( num1 ) ) );
To sort both arrays corresponding — Arrays.sort()
will not work:
index1 = 0; // defined as class variable
index2 = 0; // defined as class variable
Arrays.stream( contests[1] ).map( (n) -> new Integer[] {
contests[0][index1], contests[1][index1++]
} ).sorted( (arr1,arr2) -> Integer.compare( arr2[1], arr1[1] ) )
.forEach( (arr) -> {
contests[0][index2] = arr[0];
contests[1][index2++] = arr[1];
} );
One way is to pair the corresponding values (same index) of contests[0]
with contests[1]
and map these arrays to the contest[1]
values.
After sorting write the values back in the forEach
loop.