Sort::Key::Natural will do the trick assuming you can use CPAN (see Yes, even you can use CPAN for tips on how), but there are a couple concepts here that are probably worth learning in any case:

sort lets you define a custom sort routine, like this:
use strict; use warnings; my @aChapters=("ch1","ch11","ch2","ch5","ch55","ch16"); my @aSorted = sort { my ($sA, $iA) = ($a =~ /(^[^\d]+)(\d+)/); my ($sB, $iB) = ($b =~ /(^[^\d]+)(\d+)/); my $x = $sA cmp $sB; ($x = $iA <=> $iB) unless $x; $x; } @aChapters; local $"="\n"; print "@aSorted";

$a and $b are special variables that represent the two members of the list you want to sort. The main trick here is to (i) split your name into two parts: an alpha part and a numeric part (ii) compare the alpha part using the alpha comparison operator (cmp) and the numeric part using the numeric comparison operator (<=>). See perlop for more information.

Best, beth


In reply to Re: sorting an array by ELISHEVA
in thread sorting an array by Selvakumar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.