如何在Kotlin中过滤和排序字典(Map)



我试图在Kotlin中过滤和排序字典(map): by key = shouldShow。是shouldShow = true,按key = displayOrder降序排序这些对象,并获得带有州名的字符串列表。有可能做到吗?如何做到?因为我在努力,但都没用。提前谢谢。

地图:[{shouldShow=true, displayOrder=2, state=Alaska}, {shouldShow=false, displayOrder=3, state=Texas}, {shouldShow=true, displayOrder=1, state=Arizona}]

val list = listOf(
mapOf("shouldShow" to true, "displayOrder" to 2, "state" to "Alaska"),
mapOf("shouldShow" to false, "displayOrder" to 3, "state" to "Texas"),
mapOf("shouldShow" to true, "displayOrder" to 1, "state" to "Arizona")
)
val stateNames = list
.filter { it["shouldShow"] == true }
.sortedByDescending { it["displayOrder"] as Int }
.map { it["state"] as String }
println(stateNames)   // [Alaska, Arizona]

相关内容

  • 没有找到相关文章