I cannot see anything obviously wrong with the comparator as presented. (And I am sceptical about the proposed fixes: they "smell" of voodoo programming to me.)
But if the ACustomClass
class's aLong
attribute was mutable ... and it changed while you were sorting ... then this could cause the sort code to think that the comparator was violating the contract.
So ... check to see if this could be a concurrency issue, where one thread is mutating the objects in an array that another thread is trying to sort.
We spent quite a bit of time on this w/ the debugger...lots of different test cases. Couldn't get the behavior to be reproduced.
I would treat that as evidence that points to a concurrency issue ...