title: How to sort hash of arrays by array size
body: <p>I have a hash of arrays and I would like to sort them by the array size.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>use strict;
  use warnings;
  
  my %hash_array = (
      "array_1" =&gt; ["apple", "ball", "cat"],
      "array_2" =&gt; ["def", "leppard", "rocks", "too"],
      "array_3" =&gt; ["italian", "pastry", "missing", "cherry", "top"],
  );
  
  # Length of array_1
  my $array_1_size = @{$hash_array{"array_1"}};
  print "Should print three: $array_1_size\n";
  
  # Found this here: https://stackoverflow.com/questions/15722286
  # But my result remains unsorted
  foreach my $key ( sort { $hash_array{$b} &lt;=&gt; $hash_array{$a}} keys %hash_array ) {
      print "key: $key\n";
  }
  </code></pre>
  
  <p>I understand how to get an individual array's size, but I'm not sure how to combine that with a sort function.</p>
  
  <p>I copied the last foreach block from <a href="https://stackoverflow.com/questions/15722286/sort-keys-in-hash-of-array-by-the-number-of-elements-in-the-array">here</a> but that solution does not work for me as my output is not ordered. I had some questions about the last block of code:</p>
  
  <ol>
  <li>I do not see $b and $a referenced earlier. Is this a built in reference that the sort function understands?</li>
  <li>The solution appears to have worked for the original poster, but it is not working in my case. Yet our original goals are similar. What am I missing in order to get an ordered output based on array size?</li>
  <li>What is the best way to sort the hash of arrays by array size in ascending and descending order?</li>
  </ol>