Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do I sort a Hash of Hashes?

by NetWallah (Canon)
on Jan 10, 2004 at 05:55 UTC ( [id://320301]=note: print w/replies, xml ) Need Help??


in reply to How do I sort a Hash of Hashes?

The most straightforward answer is as neuroball has stated:

sort { $products{$a}->{"name"} cmp $products{$b}->{"name"} }

However....

This has some performance implications in that the hash dereference and comparison get performed many more times than necessary, as sort calls the compare routine each time it compares. This is a lot more then the number of elements to be compared.

If the number of elements is large, and you care about performance, you should look into the Schwartzian Transform algorithm, which perfoms the de-reference the minimum number of times.

Editor's Note: See, however, this discussion, which reveals that hash dereferences are not usually expensive enough to warrant the power and majesty of a Schwartzian transform...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://320301]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-19 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found