title: Perl: sort a hash by value, then by key
body: <p>Similar to this question: <em><a href="https://stackoverflow.com/questions/16659499/sort-by-subset-of-a-perl-string">Sort by subset of a Perl string</a></em></p>
  <p>I would like to sort first by value, and then by subset of the key.</p>
  <p>My <code>%hash</code>:</p>
  <pre><code> cat_02 =&gt; 0
   cat_04 =&gt; 1
   cat_03 =&gt; 0
   cat_01 =&gt; 3
  </code></pre>
  <p>The output (could be an array of the keys in this order):</p>
  <pre><code>cat_02 =&gt; 0
  cat_03 =&gt; 0
  cat_04 =&gt; 1
  cat_01 =&gt; 3
  </code></pre>
  <p>Bonus: The key secondary comparison would recognize 1234_2_C01 and being smaller than 1234_34_C01 (<a href="https://perldoc.perl.org/perlfunc#sort-LIST" rel="nofollow noreferrer">cmp</a> does not).</p>