# perl array sorting

**URL:** <https://community.unix.com/t/perl-array-sorting/259189>\
**Category:** Shell Programming and Scripting\
**Created:** [February 8, 2010, 1:31pm UTC](https://community.unix.com/t/perl-array-sorting/259189 "2010-02-08T13:31:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bsdeepu](https://community.unix.com/letter_avatar/bsdeepu/32/6_449c533f586939b6e5f246632a46421f.png) [@bsdeepu](https://community.unix.com/u/bsdeepu)\
**Post date:** [February 8, 2010, 1:31pm UTC](https://community.unix.com/t/perl-array-sorting/259189/1 "2010-02-08T13:31:36Z")

</div>

Hi All,

I have an array in perl as @match = (201001,201002,201001,201002);  
I am trying to sort this array as

```nohighlight
@match = sort(@match);
print "@match";

```

I dont see the output sorted any answers

I also tried another way, but still the results are not sorted

```nohighlight
 foreach my $match (sort { $a <=> $b } @match) {
 print "$match\n"; }

```

any help please

Thanks  
Deepak

---

<div class="post-metadata">

**Author:** ![wbathurs](https://community.unix.com/user_avatar/community.unix.com/wbathurs/32/5744_2.png) [@wbathurs](https://community.unix.com/u/wbathurs)\
**Post date:** [February 8, 2010, 1:50pm UTC](https://community.unix.com/t/perl-array-sorting/259189/2 "2010-02-08T13:50:50Z")

</div>

Assuming: @match = (201001,201002,201001,201002);

try this, which is assuming numerical sort:

```nohighlight
@sorted = sort { $a <=> $b } @match ;

```

if char based:

```nohighlight
@sorted = sort { lc($a) cmp lc($b) } @match;

```

---

<div class="post-metadata">

**Author:** ![bsdeepu](https://community.unix.com/letter_avatar/bsdeepu/32/6_449c533f586939b6e5f246632a46421f.png) [@bsdeepu](https://community.unix.com/u/bsdeepu)\
**Post date:** [February 8, 2010, 2:29pm UTC](https://community.unix.com/t/perl-array-sorting/259189/3 "2010-02-08T14:29:53Z")

</div>

Thank you for your answers,

I am not getting results for an array in the run time

for above example : i wrote a small program it works

```nohighlight
$ vi test.pl
#!/usr/bin/perl
@match = ("201001","201002","201001","201002");
@sorted = sort { $a <=> $b } @match ;
print "@sorted";

```

==\> but for my runtime array program i am not getting the right results

```nohighlight
#!/usr/bin/perl
@input=<>;
foreach (@input)
{
        if(/2010/)
        {
                @G=split /_/;
                @match = grep (/p20/i, @G);
                foreach (@match){
                s/p//;
                }
                #@match = reverse sort(@match);
                @sorted = sort { $a <=> $b } @match ;
                #@sorted = sort { lc($a) cmp lc($b) } @match;
                print "@sorted";
                #@sorted_numbers = reverse sort {$a <=> $b} @match;
                #foreach my $match (sort { $a <=> $b } @match) {
                #print "$match"; }
        }
}

```

===\> Input to the file is

```nohighlight
test_main_p201001
test_main_p201002
test_sub_p201001
test_sub_p201002

```

---------- Post updated at 11:15 AM ---------- Previous update was at 11:13 AM ----------

any help of suggestion  
Thank you so much for taking your time to answer my question.

---------- Post updated at 11:29 AM ---------- Previous update was at 11:15 AM ----------

thank you for reading... I found my mistake

===\>Here is the correct one...

```nohighlight
#!/usr/bin/perl
@input=<>;
@test=('');
foreach (@input)
{
        if(/2010/)
        {
                @G=split /_/;
                @match = grep (/p20/i, @G);
                foreach (@match){
                s/p//;
                }
                push(@test,@match);
        }
}
 @sorted = sort { $a <=> $b } @test ;
 print "@sorted";

```
