Hi Paul,

1. I don't use Python and what you guys are saying is of course
   technically correct for Groovy, but I must admit I have run into the
   same mistake in the past, intuitively assuming that a list would be
   component-wise-comparable to another list in Groovy
    1. (At least if teh lists have the same size, but one could
       potentially make the larger list win (i.e. +1 compare result) in
       case all existing elements are equal...)
2. We currently use our own comparable n-tuple class class that gives
   the expected behavior of comparing the 1st, 2nd, 3rd, etc tuple
   member until the result is != zero.
3. It might imho make sense to support this in Groovy directly, e.g.:
    1. collectionOfFourVectors.sortByList { [ it.x0, it.x1, it.x2,
       it.x3 ] }
4. It would be even nicer if the standard sort method could be used
   like this, or even make collections component-wise-comparable in
   general, but that would evidently be a much bigger change with some
   breaking change potential.
    1. (Although it might not be as bad as it looks, since the current
       behavior is pretty useseless in practice (?))

Cheers,
mg


On 20/05/2024 17:02, Paul King wrote:
This might even be more obvious:

println ([[x:1, y:100],  [x:2, y:1], [x: 2, y:500]].sort{[it.x,
it.y]}) // broken: [[x:2, y:1], [x:1, y:100], [x:2, y:500]]
println ([[x:1, y:100],  [x:2, y:1], [x: 2, y:500]].sort{ a, b -> a.x
== b.x ? a.y <=> b.y : a.x <=> b.x }) // works

On Tue, May 21, 2024 at 12:52 AM Paul King<pa...@asert.com.au>  wrote:
If you have only two dimensions, you'll get away with your solution
for small integer coordinate values. Here is a counter example with
integers:

println ([[x:  1, y: 69273666],  [x: 69273666, y: 1]].sort{[it.x,
it.y]}) // broken
println ([[x:  1, y: 69273666],  [x: 69273666, y: 1]].sort{ a, b ->
a.x == b.x ? a.y <=> b.y : a.x <=> b.x }) // works

On Tue, May 21, 2024 at 12:25 AM M.v.Gulik<mvgu...@gmail.com>  wrote:

On Mon, 20 May 2024 at 15:40, Paul King<pa...@asert.com.au>  wrote:
What sort result are you trying to achieve? There should be no need to
perform any recursion.

Main target was reordering some set of randomly ordered x,y coordinates into 
(for example*) a x,y(left to right, top to bottom) order (*:where any of the 
actual sort directions could be easily flipped).

So far "*.sort{[it.y, it.x]}" seems the best and easiest way to do this 
(despite its Float/Double caveat).

Reply via email to