Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Answer: How do I do a natural sort on an array?

( #68202=categorized answer: print w/ replies, xml ) Need Help??

Q&A > sorting > How do I do a natural sort on an array? contributed by tye

my %data; foreach my $data ( @data ) { ( my $sort= $data ) =~ s/(0*)(\d+)/ pack("C",length($2)) . $1 . $2 /ge; $data{$sort}= $data; } my @sorted= @data{ sort keys %data };
Bugs and features
  • Doesn't properly sort decimal values like 12.34
  • Sorts negative integers in reverse after positive integers
  • Requires more memory than other methods I'll be adding

Comment on Answer: How do I do a natural sort on an array?
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (18)
As of 2015-07-06 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (75 votes), past polls