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

Re: Sorting an array of hashes

Thread Previous | Thread Next
From:
Shawn
Date:
February 6, 2002 11:37
Subject:
Re: Sorting an array of hashes
Message ID:
05d801c1af45$5ad61bb0$d800a8c0@work

----- Original Message ----- 
From: "Tomasi, Chuck" <Chuck.Tomasi@plexus.com>
To: <beginners@perl.org>
Sent: Wednesday, February 06, 2002 1:17 PM
Subject: Sorting an array of hashes


> 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 }
> );
> 

I think this will do what you are looking for...

use strict;

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 }
);

for(sort { $a->{ID} <=> $b->{ID} } @AoH) {
  print $_->{ID}."\n";
}

Shawn

> -- 
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> 
> 
> 


Thread Previous | Thread Next


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