[Israel.pm] sort of tow dimensional array

Pinkhas Nisanov pinkhas at nisanov.com
Tue Jun 27 00:53:02 PDT 2006


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
>



More information about the Perl mailing list