in reply to Re: Data with Letter(s) & Number sort query
in thread Data with Letter(s) & Number sort query

Thank you for the quick suggestions. I too will be interested to hear the answer about padded.
I want the Letter part to be sorted as normal alphabetical order (possibly from z to a as an option) - also as another option it could be good to ignore the case of the letters.
The letter part sort comes before the number part.
  • Comment on Re^2: Data with Letter(s) & Number sort query

Replies are listed 'Best First'.
Re^3: Data with Letter(s) & Number sort query
by Laurent_R (Canon) on Nov 19, 2016 at 11:02 UTC
    If you want to the letters to be a secundary sort key, you can try this:
    $ perl -e 'use strict; > use warnings; > use Data::Dumper; > > my @a = qw / E1180 D250 A1180 E855 E975 A130 A250 B1105 B1255 B2480 +C1180 C1600 D1180 /; > > print "$_\n" > for map "$_->[0]$_->[1]", > sort { $a->[1] <=> $b->[1] || $a->[0] cmp $b->[0]} > map { /([A-Z]+)(\d+)/; [$1, $2]} @a; > ' A130 A250 D250 E855 E975 B1105 A1180 C1180 D1180 E1180 B1255 C1600 B2480
    Notice that A250 and D250 are now sorted alphabetically on the initial letter. For a reverse alphabetical order on the letters, change the sort line to this:
    sort { $a->[1] <=> $b->[1] || $b->[0] cmp $a->[0]}
Re^3: Data with Letter(s) & Number sort query
by Laurent_R (Canon) on Nov 19, 2016 at 12:05 UTC
    I did not have time before to give a complete answer to your questions, but if you want upper and lower case letters and ignore case for the sort:
    $ perl -e 'use strict; > use warnings; > > my @a = qw / d1180 a1180 E1180 D250 A1180 E855 E975 A130 A250 B1105 +b1255 b2480 c1180 c1600 e855 e975 a130 A250 B1105 B1255 B2480/; > > print "$_\n" > for map "$_->[0]$_->[1]", > sort { $a->[1] <=> $b->[1] || uc $a->[0] cmp uc $b->[0]} > map { /([a-zA-Z]+)(\d+)/; [$1, $2]} @a;' A130 a130 A250 A250 D250 E855 e855 E975 e975 B1105 B1105 a1180 A1180 c1180 d1180 E1180 b1255 B1255 c1600 b2480 B2480
    The letter part sort comes before the number part.
    Not sure what you mean.
      That is very useful but not quite what I wanted.
      In your output, the primary sort are the numbers and the secondary are the letters.
      I wanted it the other way around. Therefore all the data with A or a at the front would come before the data with B and so on. Therefore it would be (to start)
      A130 a130 A250 A250 A1180 B1105 b1255 B2480 etc
        I wanted it the other way around.

        Perhaps now would be a good opportunity for you to spend a little time understanding the code samples within this thread so that you could make the (minor) changes necessary to produce the specific sorting which you require.

        Hint 1: sort

        Hint 2: FAQ: How do I sort an array by (anything)?

        Hint 3: the only thing you need to change to my earlier script is the code line with the sort. The new code line should have exactly the same characters, but not quite in the same order.