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

RE: Sorting an array of hashes

Thread Previous | Thread Next
From:
Nikola Janceski
Date:
February 6, 2002 11:28
Subject:
RE: Sorting an array of hashes
Message ID:
1449413DA482D311B67000508B5A12F50592DCC5@nyexchange01.summithq.com
@sorted = sort { 
	$a->{ID} <=> $b->{ID}  ## remember that $a and $b become the element
of the array
	## so if it's a reference to a hash use a dereferencer '->' or
	# $$a{ID} <=> $$b{ID}
	# will work too!
	} @AoH;

foreach $item (@sorted){
	print $item->{ID}, "\n";
	}

-----Original Message-----
From: Tomasi, Chuck [mailto:Chuck.Tomasi@plexus.com]
Sent: Wednesday, February 06, 2002 2:18 PM
To: 'beginners@perl.org'
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 }
);

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

----------------------------------------------------------------------------
--------------------
The views and opinions expressed in this email message are the sender's
own, and do not necessarily represent the views and opinions of Summit
Systems Inc.


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