[英]How to filter and sort a List using another List in kotlin
我的代碼
val list1 = getAllCheckingPoints()
val list2 = getselectedCheckingPoints()
說 list1 屬於 Type1 而 list2 屬於 Type2
Type1 是包含 id、name、type、duration 的數據 class
Type2 是僅包含 id 和 position 的數據 class
兩者都包含匹配的 ID,但只有列表 2 包含位置
所以我創建了一個空列表
var filteredList: List<Type1> = emptyList()
filteredList = list2.flatMap { list2Item->
list1.filter { list1Item ->
list1Item.id == list2Item.id
}
}
在這里,我嘗試使過濾列表成為 Type1 列表,其中僅包含 list1 中存在於 list2 中且與 id 匹配的項目。 在這一步之后我被卡住了。
如何使用 list2 中存在的 position 值對過濾列表進行排序?
據我了解,您需要過濾掉list2
中沒有id的list1
的所有項目,並根據list2
object的某些屬性對它們進行排序。
val map = list2.associate { it.id to it.position }
val result = list1.filter { it.id in map }.sortedBy { map[it.id] }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.