I’m trying to implement a table with sorting its items. Currently I need to switch between sortedBy
and sortedByDescending
. How to make it optimal?
fun sort(index: Int) {
fun selector(it: Contract) = when (index) {
0 -> it.country.name
1 -> it.price
else -> it.price
}
items = if (index == sortIndex) items.sortedBy { selector(it) }
else items.sortedByDescending { selector(it) }
sortIndex = index
}
UPD: Hm, looks like it’s the optimal solution. I just need to add a second variable. But I’m curious of multiple internal functions.