android - kotlin - 过滤 MutableList 并在列表末尾附加其他项目
问题描述
实际上我想以这种方式对我的列表进行排序:我有一个这样的 mutableList
noteList = mutableListOf<NoteDataHolder>().apply {
notes.forEach {
add(NoteDataHolder(it))
}
}
想象一下NoteDataHolder
,Id
我想以此对我的列表进行排序Id
我的清单是这样的:[ {id=1}, {id=2}, {id=3}, {id=4} ]
当我像这样过滤我的列表时:noteList.filter { it.note?.bookId == 4 }
我只收到[ {id=4} ]
item4
最后,我想像这样得到所有物品[ {id=4}, {id=1}, {id=2}, {id=3} ]
解决方案
看起来你需要这样的东西:
fun reorderItems(input: List<NoteDataHolder>, predicate: (NoteDataHolder) -> Boolean): List<NoteDataHolder>{
val matched = input.filter(predicate)
val unmatched = input.filterNot(predicate)
return matched + unmatched
}
用来:
noteList = reorderItems (noteList!!) {it.note?.bookId == 4}