Title
d

Daniele B

05/18/2021, 9:52 AM
I would like to get a list of the values, ordered by the map key:
``val a = mapOf(2 to "W", 3 to "V", 1 to "T")``
desired result:
``val list = listOf("T","W","V")``
I was reading you can do that using
``sortedMap``
, but it doesn’t come out on the IDE.
e

eHelg

05/18/2021, 9:57 AM
Probably something along:
``````val a = mapOf(2 to "W", 3 to "V", 1 to "T")
val arranged = a.values.sorted()``````
or
``````val a = mapOf(2 to "W", 3 to "V", 1 to "T")
val arranged = a.values.sortedBy { /* your logic here */ }``````
?
My bad I read the question a bit wrong, maybe you are looking for something like:
``````val a = mapOf(2 to "W", 3 to "V", 1 to "T")
val arranged = a.toSortedMap().values``````
d

Daniele B

05/18/2021, 10:01 AM
Yes, but
``toSortedMap()``
doesn’t come up. Maybe because I am using MultiPlaform?
Yes, I just checked, it works on Android, but not on CommonMain.
Any alternative to it?
e

eHelg

05/18/2021, 10:05 AM
Poor mans substitue:
``````val a = mapOf(2 to "W", 3 to "V", 1 to "T")
val arranged = a.toList().sortedBy { it.first }.map { it.second }``````
But might make more sense digging into why sortedMap doesn't work
👍 1
w

wbertan

05/18/2021, 10:05 AM
🤔 Do you have TreeMap? Because the
``toSortedMap``
extension just does this:
``public fun <K : Comparable<K>, V> Map<out K, V>.toSortedMap(): SortedMap<K, V> = TreeMap(this)``
d

Daniele B

05/18/2021, 10:08 AM
@eHelg is
``sortedMap``
supported to work on MultiPlatform? uhh, that’s weird. It’s not there.
This is the definition of my map:
``val myMap = MutableMap<Int, MyObject> = mutableMapOf()``
e

eHelg

05/18/2021, 10:09 AM
No idea, never used MultiPlatform
w

wbertan

05/18/2021, 10:15 AM
``toSortedMap``
is only for JVM. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/
👍 1
m

Michael Böiers

05/19/2021, 7:08 AM
Creating a sorted map just for the purpose of sorting seems wasteful to me. Why not use a sequence of pairs, sort them by key and map the value?
v

Vampire

05/19/2021, 9:55 AM
That's the solution eHelg suggested 🙂
m

Michael Böiers

05/19/2021, 10:02 AM
Yes, but without creating a map in the process.
v

Vampire

05/19/2021, 10:03 AM
The map is the given thing
m

Michael Böiers

05/19/2021, 10:04 AM
In the example, yes.
If the map is a given, I‘d still use sequence over list :-)
👍 1
t

Tobias Berger

05/19/2021, 10:54 AM
Be aware of this if you consider sequences here: sorting a sequence still creates a list instance in the background and then builds a sequence on that list. If you're fine with a sequence as an end result, then you get a slight benefit from mapping the values out of the sorted entries. But if you need to return any kind of collection, using Sequences just creates unnecessary wrapper instances in this case.
m

Michael Böiers

05/19/2021, 10:58 AM
Considering that the wrapper instance is really cheap, I’d always try to avoid creating collection explicitly. When you go the sequence route, the implementation can decide whether to use a list or not. Of course for optimum memory performance we could use a mutable list, sort it in-place and then map the values in place, doing everything creating just one collection.
t

Tobias Berger

05/19/2021, 11:31 AM
Fair point, although something like sorting is never possible on the fly in a sequence. Using the collections directly shows more clearly what the code actually does.