I have an unordered map:
class O(val a: Int)
Map[String, List[O]]
which I'd like to turn into:
SortedMap[String, SortedMap[Int, O]]
with the child SortedMap keyed on the O field.
I'm sure there must be a more idiomatic code than the below...
class O(val a: Int)
val a: Map[String, List[O]] = Map[String, List[O]]( ("b" -> List(new O(3), new O(2))), "a" -> List(new O(1), new O(2)))
val key1s = a map (_._1)
val oMapsList = ListBuffer[SortedMap[Int, O]]()
for (key1 <- key1s) {
val oList = a(key1)
val key2s = oList map (_.a)
val sortedOMap = SortedMap[Int, O]() ++ (key2s zip oList).toMap
oMapsList += sortedOMap
}
val sortedMap = SortedMap[String, SortedMap[Int, O]]() ++ (key1s zip oMapsList).toMap
Expected sortedMap contents is:
"a" -> ( (1 -> O(1)),(2 -> O(2)) )
"b" -> ( (2 -> O(2)),(2 -> O(3)) )