didn't know where to ask, so i'll ask here. can so...
# functional
o
didn't know where to ask, so i'll ask here. can someone see what's (obviously, probably) wrong with this? i'm attempting to sort some polygons by their lowest z-coordinate. the nasty code looks like this:
Copy code
val sortedFaces: List<Pair<Int, List<Int>>> get() {
        return faces.mapIndexed { i, it ->
            Pair(i, it)
        }.sortedByDescending { indexedFace ->
            indexedFace.second.minByOrNull {
                vertices[it].z
            }
        }.also { 
            it.forEach {
                println("${it.first}: ${it.second.map { vertices[it].z }.min() }")
            }
            println()
        }
    }
and prints something like this:
Copy code
5: 0.535865025190621
3: -1.1402821230261972
0: -1.3581312119491156
2: -1.3581312119491156
4: -1.3581312119491156
1: -0.7537141141135403
how can it possibly print a wrongly sorted list here?
r
I think the
minByOrNull
is the issue... It returns the value of the
face
with the smallest value for
vertices[it].z
, instead of the smallest
z
value. If you use
minOfOrNull
instead, the output is probably what you expect it to be.
K 1
y
Do you have a runnable example we can debug? I have no clue what could be going wrong here
o
@Riccardo Lippolis oh, that makes a ton of sense. thanks, didn't know about minOfOrNull. and good job seeing what was going on.
🙌 3