I have two lists
list1 = [ { city: 'Los Angeles', population: '4 million ' } ... ]
list2 = [ { nameOfCity: 'New York', language: 'English' }, {nameOfCity: 'Berlin', language: 'German' ]
Now I need to create a new list and filter the two lists by the name of city and then group it by the same language
newClass(val nameOfCities: List<String>, language: String)
So I need to check if city names of list2 are inside list 2 (list2.filter { it.nameOfCity != list1.city } and store all the names inside the nameOfCities list grouped by language
so the end result should be something like this:
[
{ nameOfCities: [New York, Chicago]
language: Engilsh
},
{ nameOfCities: [Berlin]
language: German
}
]
So I get a new list of all the cities that don't exists in list1 and grouped by the same language.
This is the closest I got:
for (item in list1) {
reducedList = (list2.filter { it.nameOfCity != item.city }.map { newClass(it.nameOfCity, it.language) })
.toList().groupBy { it.language }
}
But the format is like this:
'english': [
{ nameOfCity: 'Los Angeles', language: 'English' },
{ nameOfCity: 'New York', language: 'English' },
],
'german': [ { nameOfCity: 'Berlin ', language: 'German' } ]