Tuesday, July 6, 2010

Sorting an Array of Hashes in Perl

I needed to sort an array of hashes in Perl (actually, it was an arrayref of hashrefs).  A quick Google search led me to this page:

With a little modification of the example, I had something that worked perfectly:

my @{ $sorted } = sort { $b->{key_field} <=> $a->{key_field} } @{ $data };

No comments:

Post a Comment