No, this is quite efficient. Hash accesses are very fast.

But if you really think you need every cycle, you might try the following

use warnings; use strict; my %hash= ('a' => {'Size' => 40 }, 'b' => {'Size' => 3000 }, 'c' => {'Size' => 20 }, 'd' => {'Size' => 1 }, 'e' => {'Size' => 12 } ); my @unsort= map ( $hash{$_}->{"Size"} .':' . $_ , keys %hash); no warnings; my @sort= sort {$a<=>$b } @unsort; use warnings; foreach (@sort) { s/[^:]*://; } print join' ',@sort,"\n"; # prints d e c a b

You really should test whether you get any speedup through this, you need fairly large hashes to see any difference at all, as you trade two hash accesses for two string-to-int conversions per sort operation. I'm not sure what will win

Instead of turning off the warnings you could also normalize the size numbers for the sorting by putting '0' before each number so that all numbers are the same size and extract the numbers in the sort routine, but that will definitely eat up any speedup over your original sort

UDPATE: Yes, forgot the Schwarzian Transform, again.

In reply to Re^3: What are the other option for sorting the keys of hash of hashes by jethro
in thread What are the other option for sorting the keys of hash of hashes by luckypower

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.