 <pre><code>x = {"job" =&gt; 1, "big job" =&gt; 2, "super job" =&gt; 1, "work" =&gt; 2, "super big job" =&gt; 1} p x.sort_by{|x,|-x.split.size}.to_h #=&gt; {"super big job"=&gt;1, "big job"=&gt;2, "super job"=&gt;1, "work"=&gt;2, "job"=&gt;1} </code></pre>