title: Sorting multidimensional hashes in perl by value within a specific key
body: <p>Thanks in advance for the help.</p>
  
  <p>I have a array called <code>%pepHash</code> with 174 'counts' and multiple 'peptides' in each <code>$count</code>.
  Here's a snippet of the hash:</p>
  
  <pre><code>Count: 39   Peptide: 0  Score: 55.03    MR: 1792.3206
  Count: 39   Peptide: 1  Score: 75.22    MR: 1792.6158
  Count: 39   Peptide: 2  Score: 62.63    MR: 1972.7156
  Count: 39   Peptide: 3  Score: 49.95    MR: 2365.2174
  Count: 40   Peptide: 0  Score: 46.38    MR: 1256.4437
  Count: 40   Peptide: 1  Score: 71.07    MR: 1950.1644
  Count: 40   Peptide: 2  Score: 71.77    MR: 2492.9394
  Count: 40   Peptide: 3  Score: 67.28    MR: 2493.0154
  </code></pre>
  
  <p>Firstly, I'm not very good with data structures, so any better advice on multi-d hashes would be nice.</p>
  
  <pre><code>Count: 40   Peptide: 1  Score: 71.07    MR: 1950.1644 
  </code></pre>
  
  <p>is accessed by <code>$pepHash[$count][$pepCount]{$PEP_SCORE}</code> or <code>{$PEP_MR}</code>, and in this case it's obviously <code>$pepHash[40][1]{$PEP_SCORE}</code>.
  I want to sort all of <code>%pepHash</code> by its scores in descending order or create a new sorted hash so the list becomes:</p>
  
  <pre><code>Count: 39   Peptide: 0  Score: 75.22    MR: 1792.6158  
  Count: 39   Peptide: 1  Score: 62.63    MR: 1972.7156 
  Count: 39   Peptide: 2  Score: 55.03    MR: 1792.3206 
  Count: 39   Peptide: 3  Score: 49.95    MR: 2365.2174
  Count: 40   Peptide: 0  Score: 71.77    MR: 2492.9394 
  Count: 40   Peptide: 1  Score: 71.07    MR: 1950.1644
  Count: 40   Peptide: 2  Score: 67.28    MR: 2493.0154
  Count: 40   Peptide: 3  Score: 46.38    MR: 1256.4437
  </code></pre>
  
  <p>Notice the updated <code>$pepCount</code> values. I have this loop which could be of use:</p>
  
  <pre><code>   for ($count = 0; $count &lt; $total; $count++) {
        for ($pepCount = 0; $pepCount &lt; $pepTotal[$count]; $pepCount++) {
        }
     }
  </code></pre>
  
  <p>I was also messing with this to no avail cause I'm a noob:</p>
  
  <pre><code>foreach $key (sort {$pepHash{$b} &lt;=&gt; $pepHash{$a}} keys(%pepHash)) {
     #CONFUSED
  }
  </code></pre>
  
  <p>EDIT: This helped me a lot: <a href="http://www.stathis.co.uk/computers/perl-sort-tutorial" rel="nofollow">http://www.stathis.co.uk/computers/perl-sort-tutorial</a></p>