I have an ArrayList in Java and each element in the list is an object with 3 fields (a, b and c). I should order by a in ascending order; if 2 elements have the same value for a, they should be ordered by b in descending order; finally, if 2 elements have the same value even for b, they should be ordered by c in ascending order.
I tried other solutions posted on stackoverflow that are based on Comparator, but I did not get to order in descending order.
Could anyone kindly help me? Many thanks!
Comparator.reversed() for descending order
In addition to the
reversedmethod I am exploiting the fact thatthenComparing()is overloaded: onethenComparing()takes aComparatoras argument, which we need for reversing, the other just takes a method reference (or lambda) as argument (aFunctionin the declaration ofthenComparing()).If either
a,borcis a primitiveint,longordoubleremember to usecomparingInt(),comparingLong(),comparingDouble(),thenComparingInt(), etc.