Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: group based array sort

by Limbic~Region (Chancellor)
on Feb 19, 2004 at 19:55 UTC ( #330335=note: print w/ replies, xml ) Need Help??


in reply to group based array sort

bageler,
I am pretty sure this is easier than it sounds. You have an AoH and want to sort it based off one of the hash values. Try the following code and see if it does what you want:

@list = sort { $a->{level} <=> $b->{level} } @list; # Print code goes here
Cheers - L~R

Update: After re-reading your problem I realized the issue is not in the sorting but in the printing, which I conveniently left out. Since a great solution has already been provided below, I will only point to my iterative solution by using a stack.


Comment on Re: group based array sort
Download Code
Re: Re: group based array sort
by benn (Priest) on Feb 19, 2004 at 20:22 UTC
    bageler seems to be looking for something more complicated that that, I'm afraid - that would simply give
    foo ->bar ->narf -->trof -->etc.
    I don't know if this is possible without recursion...being lazy, I'd personally use a recursive "GetChildren($id)" routine which greps for the parent $id and pushes the returned list onto itself - terribly inefficient (as you'd be grepping the list for every element), but easier to code :)

    Cheers,
    Ben.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2014-09-02 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls