Paul Clapham wrote:You need to decide whether you want a Comparator<Integer> or a Comparator<Test>. Your posted code uses the former but your error messages are based on code which uses the latter.
(Hint: to sort a List<Integer> you need a Comparator<Integer>.)
So once you have that straightened out, could you post the corrected code and the error messages which it produces?
Hi Paul,
I just updated my code. It should be Comparator<Test>, not Comparator<Integer>. Sorry, that was a typo.
This is what I thought of last night. On line 21, the chained reversed() is too deep down the chain method call. x-> x.getNum() needs to know the type Test in order to call x.getNum()
Comparator<Test> comparator = Comparator.comparing( (Test) x -> x.getNum() ).reversed();
However, without the chained method call reversed(),
Comparator<Test> comparator = Comparator.comparing( x -> x.getNum()); // compile ok
This line alone can inferred the Comparator<Test> from the target assignment.
Let me know whether or not my understanding is valid.
Thanks!