[Israel.pm] sort of tow dimensional array

Oron Peled oron at actcom.co.il
Tue Jun 27 13:08:10 PDT 2006


Why not a simple sort with modified predicate?

my @sortArray = sort { $a->[0] <=> $b->[0] } @array;

On Tuesday, 27 בJune 2006 10:53, Pinkhas Nisanov wrote:
> you need  "Schwartzian Transform" ( map-sort-map ):
> http://en.wikipedia.org/wiki/Schwartzian_Transform
> e.g.:
> my @array =(
> [100,'text_0'],
> [109,'text_1'],
> [80, 'text_n'] );
> 
> my @sortArray =
> map { $_->[1] }
> sort { $a->[0] <=> $b->[0] }
> map { [ $_->[0], $_ ] } @array;
> 
> 
> thanks
> Pinkhas Nisanov
> 
> On 6/26/06, rami doqa <rod082 at yahoo.com> wrote:
> > hello . i am trying to sort the tow dimensional array
> > with depending on the element [0] for each row:
> > ex:
> >
> > my @array = ();
> >  $array[0] = [100,'text_0'];
> >  $array[1] = [109,'text_1'];
> >  $array[n] = [80, 'text_n'];
> >
> > when I do the sorting like this:
> >
> > foreach $row(sort {$a <=> $b} @array)
> > {
> >    print $row->[0] . "," . $row->[1];  # i want to
> > #sort by the first index which must be $row->[0] = 80
> > }
> >
> > i noticed that the array is sorted ascending depending
> > on the index of each row
> >
> > so i need a help how can i sort by the $array[$i][0]
> > for each rows of the array.
> >
> > regards,
> > Dakka, Rami
> >
> >
> > __________________________________________________
> > Do You Yahoo!?
> > Tired of spam?  Yahoo! Mail has the best spam protection around
> > http://mail.yahoo.com
> > _______________________________________________
> > Perl mailing list
> > Perl at perl.org.il
> > http://perl.org.il/mailman/listinfo/perl
> >
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
> 
> 

-- 
Oron Peled                             Voice/Fax: +972-4-8228492
oron at actcom.co.il                  http://www.actcom.co.il/~oron
ICQ UIN: 16527398

"In theory, there is no difference between theory and practice. In practice, 
there is."
        -- Yogi Berra



More information about the Perl mailing list