title: Sorting iterable values in Spark
body: <p>Let's say I have this input data:</p>
  
  <pre><code>["example.com", Date(2000, 1, 1)] : 100,
  ["example.com", Date(2000, 2, 1)]: 30,
  ["example.com", Date(2000, 3, 1)]: 5, 
  ["xyz.com", Date(2000, 1, 1)]: 20,
  ["xyz.com", Date(2000, 2, 1)]: 10,
  ["xyz.com", Date(2000, 3, 1)]: 60]
  </code></pre>
  
  <p>I want to group by the date (descending) and then sort by the count, giving me an ordered list of domains per date. </p>
  
  <p>I want to end up with:</p>
  
  <pre><code>Date(2000, 1, 1), [["example.com", 100], ["xyz.com", 20]]
  Date(2000, 2, 1), [["example.com", 30], ["xyz.com", 10]]
  Date(2000, 3, 1), [["xyz.com", 60], ["example.com", 5]]
  </code></pre>
  
  <p>It seems like a normal use case but I can't see a way of doing this from the programming guide.</p>
  
  <p>I can <code>map</code> <code>[[domain, date] count] -&gt; [date, [domain, count]]</code></p>
  
  <p>which would give me <code>(K, V)</code> pairs</p>
  
  <pre><code>Date(2000, 1, 1), ["example.com", 100],
  Date(2000, 2, 1), ["example.com", 30],
  Date(2000, 3, 1), ["example.com", 5], 
  Date(2000, 1, 1), ["xyz.com", 20],
  Date(2000, 2, 1), ["xyz.com", 10],
  Date(2000, 3, 1), ["xyz.com", 60]
  </code></pre>
  
  <p>then <code>groupByKey</code>, giving me <code>(K, Iterable&lt;V&gt;)</code> pairs</p>
  
  <pre><code>[Date(2000, 1, 1), [["example.com", 100], ["xyz.com", 20]]
  [Date(2000, 2, 1), [["example.com", 30], ["xyz.com", 10]]
  [Date(2000, 3, 1), [["example.com", 5], ["xyz.com", 60]]
  </code></pre>
  
  <p>How can I then sort within the keys?</p>
  
  <p>Please excuse the pseudocode, I'm using the Flambo Clojure wrapper and I don't want to rewrite it in Java just to ask this question!</p>
  
  <p><strong>EDIT:</strong> Each Iterable (i.e. the list of domains) is probably going to be too large to fit in memory.</p>
  
  <p><strong>EDIT2:</strong> This is all psuedocode. I used month names to make this readable, but I've changed that to real dates for clarity.</p>