how do I sort a collection by several properties b...
# getting-started
o
how do I sort a collection by several properties but preserving the sort as i go
I assume "preserving the sort as i go" means "stable sorting".
You'll probably end up with,
Copy code
yourCollection.apply {
    sortBy { it.name }
    sortBy { it.age }
    sortBy { it.otherThing }
}
o
yes i htought so, someone was doubting me, i wanted to make sure
i
@Dominaezzz This way you end up having the collection sorted first by
otherThing
then for elements with equal
otherThing
by
age
and then by
name
d
Yeah, I wasn't too sure what "preserving the sort as i go" meant.
k
It wasn’t clear to me how you know the sorts are stable, so for anyone else looking at this check out the issue here: https://youtrack.jetbrains.com/issue/KT-12473 And the release notes here: https://blog.jetbrains.com/kotlin/2019/01/kotlin-1-3-20-released/ So the take away is that if you are targeting javascript, you need to be on at least 1.3.20 to garauntee your sorts are stable, otherwise it’s up to the browser implementation