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

2024-05-19 Thread Paul King
I am not 100% sure what you are after. If you are wanting sort by x
and then y, you just want something like this:

sorttest.sort{ a, b -> a.x == b.x ? a.y <=> b.y : a.x <=> b.x }
sorttest.sort{ a, b -> a.x == b.x ? b.y <=> a.y : b.x <=> a.x } //
reverse of above

On Mon, May 20, 2024 at 6:43 AM M.v.Gulik  wrote:
>
> Hmm. Same deal with Float values.
>
> (Guess I'm down to enforce BigDecimal values in case of sorting with
> Float or Double 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).