Scala map sorting -
how sort map of kind:
"01" -> list(34,12,14,23), "11" -> list(22,11,34)
by beginning values?
one way use scala.collection.immutable.treemap, sorted keys:
val t = treemap("01" -> list(34,12,14,23), "11" -> list(22,11,34)) //if have map... val m = map("01" -> list(34,12,14,23), "11" -> list(22,11,34)) //... use val t = treemap(m.toseq:_*)
you can convert seq or list , sort it, too:
//by specifying element sorting m.toseq.sortby(_._1) //sort comparing keys m.toseq.sortby(_._2) //sort comparing values //by providing sort function m.toseq.sortwith(_._1 < _._1) //sort comparing keys
there plenty of possibilities, each more or less convenient in context.
Comments
Post a Comment