我正在嘗試在 Kotlin 中過濾和排序字典(地圖):按鍵 = shouldShow。是 shouldShow = true,通過 key = displayOrder 對這些物件進行降序排序,得到帶有狀態名稱的字串串列。有可能做到嗎?怎么做?因為我正在嘗試,但沒有任何效果。提前致謝。
地圖:[{shouldShow=true, displayOrder=2, state=Alaska}, {shouldShow=false, displayOrder=3, state=Texas}, {shouldShow=true, displayOrder=1, state=Arizona}]
uj5u.com熱心網友回復:
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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496481.html