kotlin - 过滤 MutableList 并在列表末尾附加其他项目
kotlin - filter MutableList with append other items in the end of list
实际上我想按以下方式对我的列表进行排序:
我有一个像这样的 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}
实际上我想按以下方式对我的列表进行排序: 我有一个像这样的 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}