soubalaji has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I want to sort this
@articleid=qw(b1 b2 b3 a100 a5 a8 a10 a11);
any body can help to sort this.
Regards,
Balaji S

Replies are listed 'Best First'.
Re: How to sorting
by pseudomonas (Monk) on Oct 22, 2008 at 09:43 UTC
    You may have to provide some more information on how you want things sorted, but does @articleid = sort qw(b1 b2 b3 a100 a5 a8 a10 a11); do what you want?
Re: How to sorting
by GrandFather (Saint) on Oct 22, 2008 at 09:57 UTC
Re: How to sorting
by pseudomonas (Monk) on Oct 22, 2008 at 09:58 UTC
    Applying my powers of guesswork - do you want something like this:

    use strict; use warnings; my @articleid = sort {&byletterandnumber} qw(b1 b2 b3 a100 a5 a8 a10 a +11); use Data::Dumper; print Dumper(\@articleid); sub byletterandnumber { my($aletter, $anumber, $bletter, $bnumber); if($a =~ /([a-z]+)(\d+)/){ ($aletter, $anumber) = ($1, $2)} if($b =~ /([a-z]+)(\d+)/){ ($bletter, $bnumber) = ($1, $2)} return ($aletter cmp $bletter) || ($anumber <=> $bnumber); }
    Edit: this sorts firstly by the letter part, and then by the number part, giving a5, a8, a10, a11, a100, b1, b2, b3 in this case.
Re: How to sorting
by McDarren (Abbot) on Oct 22, 2008 at 12:30 UTC
    Ditto for comments from other respondents regarding you not specifying _how_ you want it sorted.

    However, for "natural" sorting (assuming that's what you want), I quite like Sort::Naturally's nsort function. It sorts things.... well, naturally :)

    #!/usr/bin/perl use strict; use warnings; use Sort::Naturally; my @articleid=qw(b1 b2 b3 a100 a5 a8 a10 a11); print join("\n", nsort @articleid);
    Gives...
    a5 a8 a10 a11 a100 b1 b2
    Cheers,
    Darren
Re: How to sorting
by johngg (Canon) on Oct 22, 2008 at 12:00 UTC
    Assuming that you want the letter part sorted before the number part and expanding the requirement slightly to cope with invalid IDs, you could use a kind of Schwartzian Transform. In addition to the fields you want to sort on, I include a field for validity. You can then either exclude the invalid IDs or sort them to the end of the array.

    #!/usr/bin/perl -l # use strict; use warnings; my @articleIDs = qw{ b1 27 b2 b3 a100 ab35 b17 a5 a8 a10 a11 badID }; my @sortedGoodIDs = map { $_->[ 0 ] } sort { $a->[ 2 ] cmp $b->[ 2 ] || $a->[ 3 ] <=> $b->[ 3 ] } grep { $_->[ 1 ] } map { [ $_, m{^([a-z]+)(\d+)$} ? 1 : 0, $1, $2 ] } @articleIDs; print for @sortedGoodIDs, q{-} x 15; my @sortedAllIDs = map { $_->[ 0 ] } sort { $b->[ 1 ] <=> $a->[ 1 ] || $a->[ 2 ] cmp $b->[ 2 ] || $a->[ 3 ] <=> $b->[ 3 ] } map { [ $_, m{^([a-z]+)(\d+)$} ? ( 1, $1, $2 ) : ( 0, q{}, 0 ) ] } @articleIDs; print for @sortedAllIDs, q{-} x 15;

    The output.

    a5 a8 a10 a11 a100 ab35 b1 b2 b3 b17 --------------- a5 a8 a10 a11 a100 ab35 b1 b2 b3 b17 27 badID ---------------

    I hope this is of interest.

    Cheers,

    JohnGG

Re: How to sorting
by brsaravan (Scribe) on Oct 22, 2008 at 10:02 UTC
    Take a look at this link "Sorting Alphanumeric Arrays".... http://www.perlmonks.org/?node_id=466268

      You can provide that link as [id://466268] (Sorting Alphanumeric Arrays) which allows you to check the link during preview and for others to simply click the link instead of copy and pasting it.


      Perl reduces RSI - it saves typing
        oh sorry...I didn't know this...In future I'll provide the id (as you told) instead of hard link.
Re: How to sorting
by Cristoforo (Curate) on Oct 22, 2008 at 16:15 UTC
Re: How to sorting
by luckypower (Beadle) on Oct 22, 2008 at 09:45 UTC
    use sort @articleid;...what u say ..???
    or what is your desire output..???
Re: How to sorting
by luckypower (Beadle) on Oct 22, 2008 at 10:26 UTC
    @nums = @caps = (); for (@articleid) { /([a-z]+)(\d+)/; push @caps, $1; push @nums, $2; } @newarr1 = @articleid[ sort { $nums[$a] <=> $nums[$b]} 0..$#articleid] +; @result = @newarr1[sort { $caps[$a] cmp $caps[$b]} 0..$#newarr1];