[Purdue-pm] How to sort one dimension of a 2d array

Phillip San Miguel pmiguel at purdue.edu
Fri Jan 29 07:56:40 PST 2010


Phillip San Miguel wrote:
>
>>  
> That said, the syntax you provide, in practice, does do exactly what I 
> needed...
Spoke too soon, no that isn't what I meant. Discarding the hash 
part--which isn't the problem. I have a 2D array. If I sort it your way, 
I get:

perl -e '@a=([7,0,25,10],[0,9,7,12],[3,2,1,0]);@sorted=sort {$a->[0] <=> 
$b->[0]} @a ;use Data::Dumper; print Dumper \@a;print Dumper \@sorted;'


$VAR1 = [
          [
            7,
            0,
            25,
            10
          ],
          [
            0,
            9,
            7,
            12
          ],
          [
            3,
            2,
            1,
            0
          ]
        ];
$VAR1 = [
          [
            0,
            9,
            7,
            12
          ],
          [
            3,
            2,
            1,
            0
          ],
          [
            7,
            0,
            25,
            10
          ]
        ];

But what I want is:
$VAR1 = [
          [
            0,
            0,
            1,
            0
          ],
          [
            3,
            2,
            7,
            10
          ],
          [
            7,
            9,
            25,
            12
          ]
        ];




More information about the Purdue-pm mailing list