Hey Jetinder,
you probably want to do something like:
val m = Map("z" -> 2, "c" -> 1, "b" ->
3, "a" -> 2, "t" -> 5)
import scala.collection.immutable.ListMap
ListMap.empty ++ m.toIndexedSeq.sortBy(kv => (-kv._2,
kv._1))
A bit of a warning, though. Keep in mind that the ListMap
interface leaves something to be desired if you are using big sets
of data.
Its complexity is O(n) because it needs to scan through all of its
elements until it actually does find something it needs.
I would wholeheartedly recommend using the ever so wonderful
LinkedHashMap instead - if you need a fast insertion / lookup:
import scala.collection.mutable.LinkedHashMap
LinkedHashMap.empty ++
m.toIndexedSeq.sortBy(kv => (-kv._2, kv._1))
If you are annoyed by the fact that it's a mutable collection, you
can use my immutable insertion-order-retaining collections (IORC)
at:
https://github.com/melezov/pgscala/blob/2.10.x/iorc/src/main/scala/org/pgscala/iorc/
libraryDependencies += "org.pgscala" %% "pgscala-iorc" % "0.1.7-1"
Regards,
Marko