How to sort grouped API list in LazyColumn?

24 Views Asked by At

I have a sports API list that returns a list of fixtures by date. I have successfully made these fixtures appear in separate groups by league. The problem here is that I don't know how to sort those league groups on condition  (ex., whether the user makes them favorites, high priority, or followed). Below is a JSON response design example and the lazy column. If anyone can help with that.

{
    "get": "fixtures",
    "parameters": {
        "date": "2024-03-06",
        "season": "2023",
        "league": "2"
    },
    "errors": [],
    "results": 2,
    "paging": {
        "current": 1,
        "total": 1
    },
    "response": [
        {
            "fixture": {
                "id": 1149511,
                "referee": "E. Eskås",
                "timezone": "UTC",
                "date": "2024-03-06T20:00:00+00:00",
                "timestamp": 1709755200,
                "periods": {
                    "first": 1709755200,
                    "second": 1709758800
                },
                "venue": {
                    "id": 555,
                    "name": "Etihad Stadium",
                    "city": "Manchester"
                },
                "status": {
                    "long": "Match Finished",
                    "short": "FT",
                    "elapsed": 90
                }
            },
            "league": {
                "id": 2,
                "name": "UEFA Champions League",
                "country": "World",
                "logo": "",
                "flag": null,
                "season": 2023,
                "round": "Round of 16"
            },
            "teams": {
                "home": {
                    "id": 50,
                    "name": "Manchester City",
                    "logo": "",
                    "winner": true
                },
                "away": {
                    "id": 400,
                    "name": "FC Copenhagen",
                    "logo": "",
                    "winner": false
                }
            },
            "goals": {
                "home": 3,
                "away": 1
            },
            "score": {
                "halftime": {
                    "home": 3,
                    "away": 1
                },
                "fulltime": {
                    "home": 3,
                    "away": 1
                },
                "extratime": {
                    "home": null,
                    "away": null
                },
                "penalty": {
                    "home": null,
                    "away": null
                }
            }
        },
        {
            "fixture": {
                "id": 1149513,
                "referee": "D. Massa",
                "timezone": "UTC",
                "date": "2024-03-06T20:00:00+00:00",
                "timestamp": 1709755200,
                "periods": {
                    "first": 1709755200,
                    "second": 1709758800
                },
                "venue": {
                    "id": 1456,
                    "name": "Estadio Santiago Bernabéu",
                    "city": "Madrid"
                },
                "status": {
                    "long": "Match Finished",
                    "short": "FT",
                    "elapsed": 90
                }
            },
            "league": {
                "id": 2,
                "name": "UEFA Champions League",
                "country": "World",
                "logo": "",
                "flag": null,
                "season": 2023,
                "round": "Round of 16"
            },
            "teams": {
                "home": {
                    "id": 541,
                    "name": "Real Madrid",
                    "logo": "",
                    "winner": null
                },
                "away": {
                    "id": 173,
                    "name": "RB Leipzig",
                    "logo": "",
                    "winner": null
                }
            },
            "goals": {
                "home": 1,
                "away": 1
            },
            "score": {
                "halftime": {
                    "home": 0,
                    "away": 0
                },
                "fulltime": {
                    "home": 1,
                    "away": 1
                },
                "extratime": {
                    "home": null,
                    "away": null
                },
                "penalty": {
                    "home": null,
                    "away": null
                }
            }
        }
    ]
}
val newList = mainViewModel.mainList.value.groupBy { it!!.league!!.id }
    if (newList.isNotEmpty()) {
        LazyColumn(
            modifier = Modifier
                .padding(3.dp)
                .fillMaxSize()
        ) {
            newList.forEach { (league, items) ->
                item {
                    TodayMatchesHeader(leagueItem = items[0]!!, league = league!!)
                }
                itemsIndexed(items,
                    itemContent = { index, item ->
                        TodayMatchesRow(
                            teamHomeName = item.teams!!.home!!.name!!,
                            teamHomeLogo = item.teams.home!!.logo!!,
                            teamHomeR = item.goals!!.home.toString(),
                            teamAwayName = item.teams.away!!.name!!,
                            teamAwayLogo = item.teams.away.logo!!,
                            teamAwayR = item.goals.away.toString(),
                            teamHomePen = item.score!!.penalty!!.home.toString(),
                            teamAwayPen = item.score.penalty!!.away.toString(),
                            time = item.fixture!!.date!!,
                            matchItem = item,
                            onClick = {
                                val intent = Intent(context, MatchActivity::class.java)
                                intent.putExtra("matchId", item.fixture.id)
                                intent.putExtra("homeId", item.teams.home!!.id)
                                intent.putExtra("awayId", item.teams.away.id)
                                context.startActivity(intent)
                            }
                        )
                    }
                )
            }
        }
    }
0

There are 0 best solutions below