Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Answer: How do I sort a Hash of Hashes?

by injunjoel (Priest)
on Oct 19, 2005 at 20:21 UTC ( #501407=categorized answer: print w/replies, xml ) Need Help??

Q&A > sorting > How do I sort a Hash of Hashes? - Answer contributed by injunjoel

Greetings all,
Just another suggestion. I would turn %products into an array and then use the Schwartzian Transform to do the sorting.
untested suggestion
my @products ( { name => "Floor Wax", wholesale => "50.00", retail => "75.00", }, { name => "Paper Towel", wholesale => "20.00", retail => "40.00", }, { name => "Hand Soap", wholesale => "30.00", retail => "65.00", } ); my @sorted_by_name = map{$_->[1]} sort {$a->[0] cmp $b->[0]} map{[$_->{name},$_]} @products my @sorted_by_wholesale = map{$_->[1]} sort {$a->[0] <=> $b->[0]} map{[$_->{wholesale},$_]} @products
Hope that helps,
-InjunJoel

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-10-19 17:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (255 votes). Check out past polls.

    Notices?