Gustav Elmgren
01/31/2022, 6:35 PMdata class DataStuff(val s: String, val i: Int)
fun main() {
val sortBy = "s"
val myList = listOf<DataStuff>()
val result = myList.sortedBy { it[sortBy] }
}
This clearly does not work, but is there any way to make it work? I won't know the value of sortBy
at runtime though...heckfyxe
01/31/2022, 6:41 PMRobert Jaros
01/31/2022, 7:07 PMJoffrey
01/31/2022, 8:41 PMsortBy
) to properties by hand using a when
expression for instance. It depends on what your overall goal isGustav Elmgren
01/31/2022, 8:50 PMsortBy
is user input, and if they key given does not exists on the collection of classes, an error would be returned I guess.
I was thinking of doing a when
expression, but for some reason I thought a customer annotation on some fields (which you can sort by) on the data class would be better. But I might doing it more complicated than what it is. KISS maybe?ilya.gorbunov
01/31/2022, 9:09 PMsortedWith
function.
Something like this: https://pl.kotl.in/6h09oG5WF