I want to do something like this: mylist.sort { it...
# getting-started
r
I want to do something like this: mylist.sort { it.popular or not } and then mylist.sort { it.name ascending } in the one list. I want the popular at the top and the rest sorted by name
s
A single sort with a comparator function that checks both popular and name sounds like the best option there I think.
a
Scratch that it didn't work. Deleting.
s
What we often do when we're sorting across multiple "keys" is to synthesize a single thing to compare on. For something like this we'd prefix the
name
with
a
for
popular
items and
z
for not
popular
items and sort on that combined string.
(but do it all in the comparator)
i
sortWith(compareBy<YourItemType> { if (it.popular) 0 else 1 }.thenBy { it.name })
s
Oh very nice!
r
Oh, I wish I'd used the ".thenBy" ... very nice... but its working and committed and pushed now