Assuming the timestamps are the keys of the map (i.e. it's a Map[Long,B]
):
myMap.toSeq.sortBy(_._1) // yields a Seq[Tuple2[Long,B]]
That is, you're converting the map to a Seq
first, so that element ordering will be preserved (maps are normally HashMap
s, with an unpredictable ordering), then sorting that sequence by the first element of the key-value tuple.
If the timestamps are the values, replace the _._1
with _._2
.
Note that if you convert the intermediate Seq
back to a Map
(e.g. with .toMap
), you'll be back in HashMap
land again, and your sort will be gone.
So, this might be more like what you want:
scala.collection.immutable.SortedMap(myMap.toSeq: _*) // yields a SortedMap[Long, B]
Here, we convert myMap
to a Seq
just so that it can be passed to the varargs factory method of SortedMap
.