title: Why does groupBy in Scala change the ordering of a list's items?
body: <p>This code is from a Scala Worksheet:</p>
  
  <pre><code>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 =&gt; x.a)                             
  // res11: scala.collection.immutable.Map[Int,List[com.dci.ScratchPatch.E]] =
  //    Map(
  //      2 -&gt; List(E(2,Two), E(2,Another Two)),
  //      1 -&gt; List(E(1,One), E(1,Another One)),
  //      3 -&gt; List(E(3,Three))
  //    )
  </code></pre>
  
  <p>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?</p>