N
N
ngapp2021-12-06 20:02:19
Kotlin
ngapp, 2021-12-06 20:02:19

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)
        )


There is a list with current tags:
var currentTags: List<BackgroundTag> = listOf(
        BackgroundTag.Yellow,
        BackgroundTag.Blue
)


For filtering, I use the expression:
val sortedBackgrounds: List<BackgroundViewPagerScreen> =
            backgrounds.filter { p -> backgrounds.any{ p.tagList == currentTags}}


Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
illuzor, 2021-12-06
@hypnogaja

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) }

J
jcmvbkbc, 2015-05-31
@jcmvbkbc

it's C++ standard library == STL + helper little things. The main reference is in the C++ language standard.
Docs for specific versions of gcc here: https://gcc.gnu.org/onlinedocs/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question