While you can sort it automagically (as jdporter already says) with just sort { $a <=> $b } @list, you also can do it in two lines (but still they're simpler than those regexps). First of all, prepare your data so you can use simpler tools to process it. For each list member you have two fields of data in one string, and for me it's a hash that looked at me from that @list.

my @list = ( '1,cat', '2,dog', '22,mouse', '11,eel', '001,elk', '13,mi +nk'); my %unsorted = map { split /,/ } @list ; my @sorted = map { $_.','.$unsorted{$_} } sort { $a <=> $b } keys %uns +orted;

But maybe we're not so lucky and our data will not be prepended with right numerical indexes. What to do? Just reverse the hash pair!

my @list = ( 'cat,1', 'dog,2', 'mouse,22', 'eel,11', 'elk,001', 'mink, +13'); my %unsorted = map { reverse split /,/ } @list ; my @sorted = map { $unsorted{$_}.','.$_ } sort { $a <=> $b } keys %uns +orted;

Now, what if we're totally unlucky and our numerical index not only sit in the end of string, but sometimes equals to another index? We will lose some data in a hash, if we just split index from string and make pairs from them! Could the code still be two lines and still solve the problem and remain readable and understandable?

my @list = ( 'cat,1', 'dog,1', 'mouse,22', 'eel,22', 'elk,001', 'mink, +13'); # as pointed by choroba, this can be put simpler # my %unsorted = map { $_ => [ reverse split /,/ ]->[0] } @list ; my %unsorted = map { $_ => [ split /,/ ]->[-1] } @list ; my @sorted = sort { $unsorted{$a} <=> $unsorted{$b} } keys %unsorted;

TIMTOWTDI!

p.s.: Updated last code as choroba pointed.

In reply to Re: How can I sort my array numerically on part of the string? by alexander_lunev
in thread How can I sort my array numerically on part of the string? by misterperl

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.