Scala - final map after groupBy / map not sorted even when initial list sorted
<p>This is a very simple code that can be executed inside a Scala Worksheet also. It is a map reduce kind of approach to calculate frequency of the numbers in the list.</p>

<p>I am sorting the list before I am starting the groupBy and map operation. Even then the list.groupBy.map operation generates a map, which is not sorted. Neither number wise nor frequency wise</p>

<pre><code>//put this code in Scala worksheet

//this list is sorted and sorted in list variable

val list = List(1,2,4,2,4,7,3,2,4).sorted

//now you can see list is sorted

list

//now applying groupBy and map operation to create frequency map    

val freqMap = list.groupBy(x =&gt; x) map{ case(k,v) =&gt; k-&gt; v.length } 

freqMap
</code></pre>