This code is from a Scala Worksheet:
case class E(a: Int, b: String)
val l = List(
E(1, "One"),
E(1, "Another One"),
E(2, "Two"),
E(2, "Another Two"),
E(3, "Three")
)
l.groupBy(x => x.a)
// res11: scala.collection.immutable.Map[Int,List[com.dci.ScratchPatch.E]] =
// Map(
// 2 -> List(E(2,Two), E(2,Another Two)),
// 1 -> List(E(1,One), E(1,Another One)),
// 3 -> List(E(3,Three))
// )
You will notice that groupBy returns a map, but that the ordering of the elements are now different to the way they were before. Any idea why this happens, and what the best way is to avoid this?