Re: Sort on linkedlists with double values (inside main list)

Paul King <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <CAMbkE7T04QvCxHVB27dYpPDt4fRnUwUcfFCydjK=UiRB33EBnw@mail.gmail.com>
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 <[email protected]> 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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.