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

Rick Westerman westerman at purdue.edu
Fri Jan 29 08:11:17 PST 2010


    So if I am not missing something here,  basically you have an  
array of arrays but then are deconstructing the data into separate  
arrays thus destroying any relationship between the data.  That seems  
strange to me.  But if so I'd map all of the data into their separate  
arrays, sort those arrays and then remap that the information into  
your array of arrays.   Or just skip that final step and deal with  
the separate arrays since the data no longer has any relationship.

-- Rick



> 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
>          ]
>        ];
>
>
> _______________________________________________
> Purdue-pm mailing list
> Purdue-pm at pm.org
> http://mail.pm.org/mailman/listinfo/purdue-pm



More information about the Purdue-pm mailing list