title: How to sort a hash by values
body: <p>I was trying to sort a particular hash by values. I came across a way using the method <code>sort_by</code>. But even though I call <code>sort_by</code> on a hash, it returns an array, i.e.:</p>
  
  <pre><code>a = {}
  a[0] = "c"
  a[1] = "b"
  a[2] = "a"
  a.sort_by{|key, value| value}
  # =&gt; [[2, "a"], [1, "b"], [0, "c"]]
  </code></pre>
  
  <p>If I try to convert the result into a hash, I end up with a hash sorted on key, hence the whole purpose of <code>sort_by</code> is lost. i.e.:</p>
  
  <pre><code>a = {}
  a[0] = "c"
  a[1] = "b"
  a[2] = "a"
  Hash[*a.sort_by{|key, value| value}.flatten]
  # =&gt; {0=&gt;"c", 1=&gt;"b", 2=&gt;"a"}
  </code></pre>
  
  <p>Is there a way I can sort a hash by value and yet get the results back in the form of a Hash?</p>
  
  <p>I am using 1.8.6 ruby</p>