Sort by a key, but value has more than one element using Scala
<p>I'm very new to Scala on Spark and wondering how you might create key value pairs, with the key having more than one element. For example, I have this dataset for baby names:</p>

<p>Year, Name, County, Number</p>

<p>2000, JOHN, KINGS, 50</p>

<p>2000, BOB, KINGS, 40</p>

<p>2000, MARY, NASSAU, 60</p>

<p>2001, JOHN, KINGS, 14</p>

<p>2001, JANE, KINGS, 30</p>

<p>2001, BOB, NASSAU, 45</p>

<p>And I want to find the most frequently occurring for each county, regardless of the year. How might I go about doing that?</p>

<p>I did accomplish this using a loop. Refer to below. But I'm wondering if there is shorter way to do this that utilizes Spark and Scala duality. (i.e. can I decrease computation time?)</p>

<pre><code>val names = sc.textFile("names.csv").map(l =&gt; l.split(","))

val uniqueCounty = names.map(x =&gt; x(2)).distinct.collect

for (i &lt;- 0 to uniqueCounty.length-1) {
    val county = uniqueCounty(i).toString;
    val eachCounty = names.filter(x =&gt; x(2) == county).map(l =&gt; (l(1),l(4))).reduceByKey((a,b) =&gt; a + b).sortBy(-_._2);
    println("County:" + county + eachCounty.first)
}
</code></pre>