[ABE.pm] sorting an AoA

Walt Mankowski waltman at pobox.com
Wed Jul 26 12:56:42 PDT 2006


On Wed, Jul 26, 2006 at 03:24:59PM -0400, Faber J. Fedor wrote:
> I've got an array of arrays, to wit:
> 
> my @array1    = ( ["company1", "1"],
>                   ["company2", "3"],
>                   ["company3", "1.5"],
>                 )
> 
> I need to numerically sort the AoA by the second column so I have
> 
> my array2  = ( ["company1", "1"],
>                ["company3", "1.5"],
>                ["company2", "3"],
>              )
> 
> Everything sort example I've seen so far returns an array of single
> values, i.e.
> 
> my @array3 = ( "1",
>                "1.5",
>                "3",
>              )
> 
> How do I get @array1 sorted into @array2?  

This seems to work:

my @array2 = sort {$a->[1] <=> $b->[1]} @array1;

Walt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20060726/07d54fe7/attachment.bin 


More information about the ABE-pm mailing list