Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
"be consistent"
 
PerlMonks  

RE: Re: Sort Array of Hashes based on Hash value

by sean (Beadle)
on Jun 07, 2000 at 01:05 UTC ( [id://16804]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Sort Array of Hashes based on Hash value
in thread Sort Array of Hashes based on Hash value

Actually...
sub this { my @array = @{(shift)} ; my $keys = shift ; for my $key (@$keys) { @array = sort {$$a{$key} cmp $$b{$key}} @array; } return @array; }
would be a little cleaner. The original code would return a sorted array AND leave you with a sorted @unsorted. This way, by dereferencing the array, it will just return a sorted array, and leave @unsorted alone.

Replies are listed 'Best First'.
RE: RE: Re: Sort Array of Hashes based on Hash value
by BBQ (Curate) on Jun 07, 2000 at 05:22 UTC
    I like the example from perlfaq4:
    Here we'll do a reverse numeric sort by value, and if two keys are identical, sort by length of key, and if that fails, by straight ASCII comparison of the keys (well, possibly modified by your locale -- see perllocale). @keys = sort { $hash{$b} <=> $hash{$a} || length($b) <=> length($a) || $a cmp $b } keys %hash;
    as our peers above have pointed out ($$a{$key}) does the work in your case, but the idea of or'ing with a final cmp will probably get better results when you can't tell what's in those arrays.

    #!/home/bbq/bin/perl
    # Trust no1!
RE: RE: Re: Sort Array of Hashes based on Hash value
by buzzcutbuddha (Chaplain) on Jun 07, 2000 at 11:45 UTC
    Awesome!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://16804]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.