luckypower has asked for the wisdom of the Perl Monks concerning the following question:

hello,

my @sort = sort {$hash{$a}->{"Size"} <=> $hash{$b}->{"Size"}} keys %hash;
is the one option for sorting the keys of hash of hashes.

what are the other option for sorting keys of hash of hashes.

Replies are listed 'Best First'.
Re: What are the other option for sorting the keys of hash of hashes
by ikegami (Patriarch) on Sep 29, 2008 at 11:16 UTC
    Your question is very ambiguous. What problem are you trying to fix?
      my @sort = sort {$hash{$a}->{"Size"} <=> $hash{$b}->{"Size"}} keys %hash;
      i think to sort keys of hash which contains another hashes using this way is very costly.

      so want to know is there any other option to do so..???
        Do you have actual performance problems? If not, don't worry about it. And if you have performance problems, profile your code first to find out which parts of the code consume the most time.

        In case of many items the hash lookups can be reduced with a Schwartzian Transform, but it will eat more memory in turn, and likely to only give you a small speedup, if any.

        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.
Re: What are the other option for sorting the keys of hash of hashes
by jethro (Monsignor) on Sep 29, 2008 at 11:25 UTC

    If you want to sort by "Size" entry, this is practically the only option. You could also store the "Size" and the key into a file and use unix sort command, but you'd have to be really creative to find a reason why to do this

    If this answer isn't enough for you, you have to tell us some more about the circumstances to get a useful answer.

Re: What are the other option for sorting the keys of hash of hashes
by Anonymous Monk on Sep 29, 2008 at 11:22 UTC
    keep an ARRAY of hashes, keep it sorted
      But i want to sort it according to the values of hashes....
      so how to sort an array of hashes according to hashes value ...??
        so how to sort an array of hashes according to hashes value ...??
        Same,
        @hash = sort {$a->{"Size"} <=> $b->{"Size"}} @hash;
        Faster still, array or arrays
        @hash = [ blah1 => 33], [ blah2 => 44 ]; @hash = sort {$a->[1] <=> $b->[1] } @hash;
        So which is more useful? flexible? easier to write maintain? Whats cheaper, your time, or computer time?