Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Sorting an array by another array

by clintp (Curate)
on Dec 05, 2001 at 00:16 UTC ( [id://129435]=note: print w/replies, xml ) Need Help??


in reply to Sorting an array by another array

Similar to Masem's approach, but simpler (I think) and debugged.
@to_be_sorted=qw( cat bird dog tiger puma mouse ); @order=qw( mouse cat dog lion elephant ); %order=(); for($_=0; $_<@order; $_++) { $order{$order[$_]}=$_; } @sorted=sort { $order{$a}=@order unless exists $order{$a}; $order{$b}=@order unless exists $order{$b}; $order{$a}<=>$order{$b} } @to_be_sorted;
Enjoy!

Log In?
Username:
Password:

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

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

    No recent polls found