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

Re: sorting an array

by missingthepoint (Friar)
on Jul 10, 2009 at 09:09 UTC ( [id://778859]=note: print w/replies, xml ) Need Help??


in reply to sorting an array

# first num sub num_from_str { my $str = shift; my ($num) = $str =~ /(\d+)/; return $num; } my @a = qw( ch1 ch11 ch55 ch5 ch2 ch16 ); my @sorted = sort { num_from_str($a) <=> num_from_str($b) } @a; print "$_\n" for @sorted;

update: ... but Sort::Key::Natural is a more complete solution, because it sorts based on the alphabetical bits as well.


The zeroeth step in writing a module is to make sure that there isn't already a decent one in CPAN. (-- Pod::Simple::Subclassing)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-16 14:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found