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

2024-05-21 Thread M.v.Gulik
On Tue, 21 May 2024 at 14:01, Paul King  wrote:

> On Tue, May 21, 2024 at 3:13 PM M.v.Gulik  wrote:
> >
> > After fixing my local bug I rechecked the "*.sort{ a, b -> a.y == b.y ?
> -a.y <=> -b.y : a.x <=> b.x }" variant.
> > Same result/conclusion.
>
> In terms of referencing the properties, you'd want to swap the order
> you have above, i.e. you'd not want to have y ? y : x but rather y ? x
> : y or x ? y : x.
>

Darn. Yea, your right. I messed it up (again) :-/ .
After fixing that (and triple checking) it worked like advertised.
Thanks again.

On Tue, 21 May 2024 at 14:01, Paul King  wrote:

> Also, the "*.sort" would only be needed if you have lists of lists of maps.
>

Ditching that ".sort{ a, b -> a.x <=> b.x }.sort{ a, b -> -a.y <=>
-b.y}" idea of mine.
(might play around with it a bit more on a rainy day to see in which cases
it would fail, just out of curiosity.)


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

2024-05-20 Thread M.v.Gulik
After fixing my local bug I rechecked the "*.sort{ a, b -> a.y == b.y ?
-a.y <=> -b.y : a.x <=> b.x }" variant.
Same result/conclusion.

However, on a hunch, I split in into two separate consecutive sorts.
"*.sort{ a, b -> a.x <=> b.x }.sort{ a, b -> -a.y <=> -b.y }"
So far this seems to do the job of fully sorting my 2d/coordinates set. (so
far: no guaranties, as the used data set is somewhat limited and specific.)


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

2024-05-20 Thread M.v.Gulik
On Mon, 20 May 2024 at 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
>

 Aha. Thanks.
Definitely needed to see it 'not' working in one of the other value types.
+(*spotte a bug in my source code that gave me the wrong result on my
"*.sort{ a, b ->...}" test. ... it picked the wrong sort-case*. :-/ )


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

2024-05-20 Thread M.v.Gulik
On Mon, 20 May 2024 at 15:40, Paul King  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*).


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

2024-05-20 Thread M.v.Gulik
Tried "**.sort{ a, b -> a.x == b.x ? a.y <=> b.y : a.x <=> b.x }*" in the
actual source code so see its effect
And it turns out its not doing the same job as "**.sort{[it.y, it.x]}*" (*not
unless, I guess, its run recursively until the list-order stopped changing
... which I'm ofc not going to do*)


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

2024-05-20 Thread M.v.Gulik
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.

>


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

2024-05-20 Thread M.v.Gulik
I'm after information so I might better understand why "**.sort{[ -it.x,
-it.y]}*" (*vs "*.sort{[ it.x, it.y]}"*) has no effect on Double and Float
values in this particular case (*both sorts return identical ordered list*.
Not so when using Integer or BigDecimal values).

Mainly working with Python (*but I have no choice to use Groovy in this
case, which is kinda alien to me*). As such I prefer "**.sort{[it.x, it.y]}*"
over "**.sort{ a, b -> a.x == b.x ? a.y <=> b.y : a.x <=> b.x }*" ... + The
latter might, for all I know at this moment, exhibit the same behavior when
it comes to Double or Float values.


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

2024-05-19 Thread M.v.Gulik
Hmm. Same deal with Float values.

(Guess I'm down to enforce BigDecimal values in case of sorting with
Float or Double values.)


Sort on linkedlists with double values (inside main list)

2024-05-19 Thread M.v.Gulik
I'm totally bewildered by the following. (And googling it did not help).

Erm ... markup ... don't know.

[code]
def sorttest = []
if(1){
sorttest = [[x:3,y:2],[x:4,y:1],[x:2,y:3],[x:1,y:4]]
// if(DBL){println('sorttest|1a: ' + sorttest)}
assert sorttest instanceof List
assert sorttest.size() == 4
assert sorttest[0] instanceof java.util.LinkedHashMap
assert sorttest[0].size() == 2
assert sorttest[0].x instanceof Integer

sorttest.sort{[it.x, it.y]}
// if(DBL){println('sorttest|1a: ' + sorttest)}
sorttest.sort{[-it.x, -it.y]}
// if(DBL){println('sorttest|1b: ' + sorttest)}

sorttest.sort{[it.y, it.x]}
// if(DBL){println('sorttest|2a: ' + sorttest)}
sorttest.sort{[-it.y, -it.x]}
// if(DBL){println('sorttest|2b: ' + sorttest)}
}
if(1){
sorttest = [[x:3,y:2],[x:4,y:1],[x:2,y:3],[x:1,y:4]]
// if(DBL){println('sorttest|0a: ' + sorttest)}
for (int i in 1..sorttest.size()){
i -= 1
sorttest[i] = [x:sorttest[i].x.toDouble(),
y:sorttest[i].y.toDouble()]
}
// if(DBL){println('sorttest|0b: ' + sorttest)}
assert sorttest instanceof List
assert sorttest.size() == 4
assert sorttest[0] instanceof java.util.LinkedHashMap
assert sorttest[0].size() == 2
assert sorttest[0].x instanceof Double

sorttest.sort{[it.x, it.y]}
// if(DBL){println('sorttest|1a: ' + sorttest)}
sorttest.sort{[-it.x, -it.y]} // reversing the output (well trying)
// if(DBL){println('sorttest|1b: ' + sorttest)}

sorttest.sort{[it.y, it.x]}
// if(DBL){println('sorttest|2a: ' + sorttest)}
sorttest.sort{[-it.y, -it.x]} // reversing the output (well trying)
// if(DBL){println('sorttest|2b: ' + sorttest)}
}

[/code]
[Output]:
sorttest|0: [[x:3, y:2], [x:4, y:1], [x:2, y:3], [x:1, y:4]]
sorttest|1a: [[x:1, y:4], [x:2, y:3], [x:3, y:2], [x:4, y:1]]
sorttest|1b: [[x:4, y:1], [x:3, y:2], [x:2, y:3], [x:1, y:4]]
sorttest|2a: [[x:4, y:1], [x:3, y:2], [x:2, y:3], [x:1, y:4]]
sorttest|2b: [[x:1, y:4], [x:2, y:3], [x:3, y:2], [x:4, y:1]]

sorttest|0a: [[x:3, y:2], [x:4, y:1], [x:2, y:3], [x:1, y:4]]
sorttest|0b: [[x:3.0, y:2.0], [x:4.0, y:1.0], [x:2.0, y:3.0],
[x:1.0, y:4.0]]
sorttest|1a: [[x:1.0, y:4.0], [x:2.0, y:3.0], [x:3.0, y:2.0],
[x:4.0, y:1.0]]
sorttest|1b: [[x:1.0, y:4.0], [x:2.0, y:3.0], [x:3.0, y:2.0],
[x:4.0, y:1.0]] -- No Change ???
sorttest|2a: [[x:4.0, y:1.0], [x:3.0, y:2.0], [x:2.0, y:3.0],
[x:1.0, y:4.0]]
sorttest|2b: [[x:4.0, y:1.0], [x:3.0, y:2.0], [x:2.0, y:3.0],
[x:1.0, y:4.0]] -- No Change ???
[/Output]

If this it something version specific (like: groovy-3.0.12) ... I
can't change that (not in this case).