If you don't mind reformatting the data slightly you can run a quick regex across the data to reverse the order of the alphanumeric and numeric data.

chomp(my @unsorted = (<DATA>)); map{ $_ =~ s/(\d+),(\w+)/$2,$1/ } @unsorted; my @sorted = sort { $a cmp $b } @unsorted; say for @sorted;

Or do you want to do a complex sort without reformatting? In which case you can do something like the below, which will sort without reformating.

chomp(my @unsorted = (<DATA>)); my @sorted = sort { (split(',',$a))[1] cmp (split(',',$b))[1] || (split(',',$a))[0] cmp (split(',',$b))[0] } @unsorted; say for @sorted;

But again, if your ultimate goal is simple sums of each alphanumeric code, there is no need to sort unless you are outputting each sum as you go and purging it from memory. If holding all the sums in memory at one time is not a problem, summing into a hash should be all you need as I did in my previous example.


In reply to Re^3: Alphanumeric sort by Kc12349
in thread Alphanumeric sort by zac_carl

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.