Hello everyone, I was trying to group list of even...
# compose
n
Hello everyone, I was trying to group list of events by day and sort the events within that day and I ran into a problem when I try to group the events for a given day by event type and still sort the whole events for that given day by start time regardless of the event type. Open to any suggestions. Thanks.
l
Copy code
eventsWithType.groupBy { it.day }.mapValues { it.value.sortedBy { event -> event.startTime } }
will group by day and sort each day’s events by startTime. To group by event type as well, wrap this with
Copy code
events.groupBy { it.eventType }.mapValues { groupUsingAboveCode(it.value) }
n
I'll give it a go. Thanks
l
I would personally create some data classes so you don’t have a bunch of maps. I see this is in the compose channel, so I’ll add that this shouldn’t be done inside a composable. The sorting should happen in your state holder (if you get events from a DB, you can set up a flow to handle the above transformation and use
collectAsState
in your compose code.
n
I do have data classes and the mapper is working fine the problem is when I sort the events one group always comes on top. Examples I have 9am type a 10am type b 11am type a This should display as it is but it always displays group 'type b'up top.