title: How to sort by value efficiently in PySpark?
body: <p>I want to sort my K,V tuples by V, i.e. by the value.  I know that <code>TakeOrdered</code> is good for this if you know how many you need:</p>
  
  <pre><code>b = sc.parallelize([('t',3),('b',4),('c',1)])
  </code></pre>
  
  <p>Using <code>TakeOrdered:</code> </p>
  
  <pre><code>b.takeOrdered(3,lambda atuple: atuple[1])
  </code></pre>
  
  <p>Using <code>Lambda</code></p>
  
  <pre><code>b.map(lambda aTuple: (aTuple[1], aTuple[0])).sortByKey().map(
      lambda aTuple: (aTuple[0], aTuple[1])).collect()
  </code></pre>
  
  <p>I've checked out the question <a href="https://stackoverflow.com/questions/24656696/spark-get-collection-sorted-by-value">here</a>, which suggests the latter.  I find it hard to believe that <code>takeOrdered</code> is so succinct and yet it requires the same number of operations as the <code>Lambda</code> solution.  </p>
  
  <p>Does anyone know of a simpler, more concise Transformation in spark to sort by value?</p>