I'm trying to write some code to sort a list of numbers by their absolute difference from a target or, if numbers are equidistant, by the value of the numbers. That is, in pseudocode,
compare( a, b, target ) {
if ( Math.abs( a - target ) < Math.abs( b - target ) )
return -1; // CompareTo convention
else if ( Math.abs( a - target ) > Math.abs( b - target ) )
return 1;
else
return compare( a, b )
I figured that I could using comparing...thenComparing to do the two-stage comparison, but the thenComparing part seems to cause compilation problems.
When I try compiling the following code:
public void fn( int k, int x ) {
Comparator< Integer > comparator1 =
Comparator.comparingInt( i -> Math.abs( x - i ) );
Comparator< Integer > comparator2 =
Comparator.comparingInt( i -> Math.abs( x - i ) ).
thenComparingInt( i -> i );
}
I find that the definition of comparator1 is fine but comparator2 gives me the following compilation messages (in Eclipse IDE):
Multiple markers at this line
- The operator - is undefined for the argument type(s) int, Object
- Type mismatch: cannot convert from Comparator<Object> to
Comparator<Integer>
Can someone please point out what I'm doing wrong?