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?

by Dominus (Parson)
on Mar 30, 2001 at 19:19 UTC ( #68394=categorized answer: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    sub byfile {
      my @a = split /(\d+)/, $a;
    ...
      }
      $res;
    }
    
  2. or download this
    53 7 119 53red
    red5 red6 red7 red67 red6.jpg red12.jpg
    green4.jpg
    blue2.jpg blue1000.jpg blue2.jpg58
    
  3. or download this
    7
    53
    ...
    red7
    red12.jpg
    red67
    
  4. or download this
     while (defined (my $A = shift @a) and defined (my $B = shift @b)) {
       $res = ($A =~ /\d/) ? $A <=> $B : $A cmp $B;
       return $res if $res;
     }
     return defined $A ? -1 : 1;
    

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[GrandFather]: Oh, and the manufacturer tells lies too! I'm sure they don't mean to, but I know for sure some of the stuff they say is just wrong. It's possible they don't understand what we are asking, but that's not a good look either
[Corion]: GrandFather: Ah, unit testing hardware is fun, especially when the hardware is uncalibrated ...
[GrandFather]: Us: "What scaling do we need to apply to the numbers from the SDK for the wibble?". Them: "Oh, the numbers from the SDK for the wibble are already correct, they don't need scaling"
[GrandFather]: In our code: wibble range 1 scale by 1, range 2 scale by 2, range 3 scale by 4, range 4 scale by 8

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2017-08-24 07:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (365 votes). Check out past polls.

    Notices?