Spark - Difference between sortBy and sortByKey
<p>What is the difference between <code>sortBy</code> and <code>sortByKey</code> functions in Spark?</p>

<p>I am performing this below transformation in which i am using sortBy &amp; sortByKey. Both are giving the same results then what is the difference in that.</p>

<pre class="lang-scala prettyprint-override"><code>val reducedSfpd = sfpd.map(x =&gt; (x(col_2),1)).reduceByKey((x,y) =&gt; x+y)

val top3Dist = reducedSfpd.sortBy(_._2,false).collect().take(3)    
val top3Dist = reducedSfpd.map(x =&gt; x.swap).sortByKey(false).take(3)
</code></pre>

<p>Is there any performance related difference between sortBy &amp;&amp; sortByKey.</p>

<p>In fact when I am using <code>sortBy</code> I am saving one transformation of swapping the 'Key - Values' by applying map function. Then why <code>sortByKey</code>?</p>