I want to sort data which has a letter or number of letters at the beginning of each value and then numbers.
I want to do this so that the number part increases by the number size.
Hopefully the example below explains what I mean.
The Perl code is
use strict "vars"; my (@a, @b, $ja); $a[1] = 'E1180'; $a[2] = 'D250'; $a[3] = 'A1180'; $a[4] = 'D130'; $a[5] = 'E855'; $a[6] = 'E975'; $a[7] = 'A130'; $a[8] = 'A250'; $a[9] = 'B1105'; $a[10] = 'B1225'; $a[11] = 'B2480'; $a[12] = 'C1180'; $a[13] = 'C1600'; $a[14] = 'D1180'; $b[1] = 'E1180'; $b[2] = 'D0250'; $b[3] = 'A1180'; $b[4] = 'D0130'; $b[5] = 'E0855'; $b[6] = 'E0975'; $b[7] = 'A0130'; $b[8] = 'A0250'; $b[9] = 'B1105'; $b[10] = 'B1225'; $b[11] = 'B2480'; $b[12] = 'C1180'; $b[13] = 'C1600'; $b[14] = 'D1180'; print "\ncmp sort of \@a\n"; foreach $ja (sort{$a cmp $b} @a) { print "$ja\n"; } print "\ncmp sort of \@b\n"; foreach $ja (sort{$a cmp $b} @b) { print "$ja\n"; }
This code gives the following output.
cmp sort of @a A1180 A130 A250 B1105 B1225 B2480 C1180 C1600 D1180 D130 D250 E1180 E855 E975 cmp sort of @b A0130 A0250 A1180 B1105 B1225 B2480 C1180 C1600 D0130 D0250 D1180 E0855 E0975 E1180
Data in @a is sorted so that the number part is treated a letters. Therefore for the values starting with A, the order of the number part is 1180, 130, 250.
What I really want for these values is the order 130, 250, 1180.
I have kind of achieved this by adding a zero in front of numbers where there value is less than 1000. This is the data in @b
However, the data then becomes A0130, A0250, A1180 which is not ideal.
Is there a way of sorting the data in @a so that I get the order shown for @b but without adding the extra and unwanted zeros?

In reply to Data with Letter(s) & Number sort query by merrymonk

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.