title: Sorting a hash in ruby based on value and then key
body: <p>How do you sort a hash in ruby based on the value and then key? For example</p>
  
  <pre><code>h = {4 =&gt; 5, 2 =&gt; 5, 7 =&gt; 1}
  </code></pre>
  
  <p>would sort into </p>
  
  <pre><code>[[7, 1], [2,5], [4, 5]]
  </code></pre>
  
  <p>I can sort based on the value by doing</p>
  
  <pre><code>h.sort {|x,y| x[1] &lt;=&gt; y[1]}
  </code></pre>
  
  <p>but I can't figure out how to sort based on value and then key if the values are the same</p>