Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by tye (Sage)
on Mar 30, 2001 at 01:07 UTC ( #68202=categorized answer: print w/replies, xml ) Need Help??

Q&A > sorting > How do I do a natural sort on an array? - Answer 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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2016-10-24 10:21 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (304 votes). Check out past polls.