I'm new to Kotlin and I have the following task. I have a list of objects (Album) which contains fields and a list with another object (Song). I need to sort both album and song lists with their properties. What I tried so far
albumList.onEach { it.songs.sortedByDescending { song -> song.isFavorite}
.sortedByDescending { it.createDate }
As a result, the album list is sorted by it's createDate property but songs list doesn't. Is there something I'm missing? Thanks
valor avar? Is it aMutableListor read-onlyList?sortedBycreates a new sorted list, but does not modify the original list. You need either a MutableList and usesortByinstead ofsortedBy. Or you need the songs list to be avarand you set the result of thesortedBycall back to the original property to change it.sortedByalways creates a new List instance.createDateinAlbumorSong? Are you planning to sort albums bycreateDate, and songs in EACH of album sort byisFavorite(so favourites first).