The one argument closure variant of sort is expecting the closure to
return a comparable. The values of any two items are sorted according
to that comparable.

ArrayLists aren't comparable. In that case the hashCode is used as a
fallback for the comparison value. The hashCode for a Java array list
is calculated in terms of the hashCodes of the individual elements and
will only work by fluke, e.g. for small values of integers.

If the floats are small, you could use something like:
sorttest.sort{ it.x * 1000 + it.y }
sorttest.sort{ it.x * -1000 - it.y }

Cheers, Paul.

On Mon, May 20, 2024 at 8:53 PM M.v.Gulik <mvgu...@gmail.com> wrote:
>
> Verified that "*.sort{ a, b -> a.x == b.x ? -a.y <=> -b.y : -a.x <=> -b.x }" 
> actually behaves ok when using Double or Float values (ie: same ordered 
> output as with Integer or BigDecimal values).
>
> Why "*.sort{[ -it.x, -it.y]}" behaved differently when using Double or Float 
> values in this case is beyond me ... Guess i just chalk it up as some 
> peculiar groovy thing.

Reply via email to