develooper Front page | perl.beginners | Postings from February 2002

Re: Sorting an array of hashes

Thread Previous
From:
Chas Owens
Date:
February 6, 2002 11:35
Subject:
Re: Sorting an array of hashes
Message ID:
1013024123.9444.25.camel@tert.icallinc.com
On Wed, 2002-02-06 at 14:17, Tomasi, Chuck wrote:
> Does anyone have any clever ideas for sorting an array of hashes based on
> a key such as an ID number?
> 
> Example:
> 
> @AoH = (
> 	{ ID => 10101, UserID => 1041, Status => 2 },
>         { ID => 10541, UserID => 1211, Status => 1 },
>         { ID => 10111, UserID => 1211, Status => 2 },
>         { ID => 10721, UserID => 1198, Status => 1 }
> );
> 

<example>
#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my @AoH = (
        { ID => 10101, UserID => 1041, Status => 2 },
        { ID => 10541, UserID => 1211, Status => 1 },
        { ID => 10111, UserID => 1211, Status => 2 },
        { ID => 10721, UserID => 1198, Status => 1 }
);

print Dumper($_) for (sort { $a->{ID} <=> $b->{ID} } @AoH);
</example>


-- 
Today is Boomtime the 37th day of Chaos in the YOLD 3168
You are what you see.

Missle Address: 33:48:3.521N  84:23:34.786W


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About