У меня есть пример для вас.
data class CartItem(
var shopId : Int,
var itemName : String
)
val list = listOf(CartItem(1,"Onion"),
CartItem(1,"Potato"),
CartItem(2,"Banana"),
CartItem(2,"Apple"))
val listOfDifferentShopIds = mutableListOf<List<CartItem>>()
val getUniqueShopIds = list.distinctBy { it.shopId }.map { it.shopId }
getUniqueShopIds.forEach{ uniqueShopID->
listOfDifferentShopIds.add(list.filter{ uniqueShopID == it.shopId })
}
print(listOfDifferentShopIds)
Результат
[[CartItem(shopId=1, itemName=Onion), CartItem(shopId=1, itemName=Potato)], [CartItem(shopId=2, itemName=Banana), CartItem(shopId=2, itemName=Apple)]]
Надеюсь, это решит вашу проблему.