Answer the question
In order to leave comments, you need to log in
How to properly filter a list of objects according to another list?
Hello everyone )
I have a simple list of backgrounds that needs to be filtered according to active tags
. I use the construction with any, but the list is always empty, how to get the filter result correctly?
val backgrounds: List<ArticleViewPagerScreen> = listOf(
BackgroundViewPagerScreen(
headerRes = R.string.header1,
dateRes = R.string.date1,
textRes = R.string.text1,
drawableRes = R.color.color1,
tagList = listOf(ArticleTag.Yellow)
),
BackgroundViewPagerScreen(
headerRes = R.string.header2,
dateRes = R.string.date2,
textRes = R.string.text2,
drawableRes = R.color.color2,
tagList = listOf(ArticleTag.Orange)
),
BackgroundViewPagerScreen(
headerRes = R.string.header3,
dateRes = R.string.date3,
textRes = R.string.text3,
drawableRes = R.color.color3,
tagList = listOf(ArticleTag.Blue)
)
var currentTags: List<BackgroundTag> = listOf(
BackgroundTag.Yellow,
BackgroundTag.Blue
)
val sortedBackgrounds: List<BackgroundViewPagerScreen> =
backgrounds.filter { p -> backgrounds.any{ p.tagList == currentTags}}
Answer the question
In order to leave comments, you need to log in
Wrong logic. listOf(ArticleTag.Blue) is not always equal to listOf(BackgroundTag.Yellow,BackgroundTag.Blue).
I'm not sure if I understood the requirements correctly, but it looks like you need to check that all tags from p.tagList are contained in currentTags :
backgrounds.filter { p -> currentTags.containsAll(p.tagList) }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question