Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting an array of hashes

59 views
Skip to first unread message

Chris Mortimore

unread,
Aug 5, 2004, 5:18:57 PM8/5/04
to begi...@perl.org
I want to sort an AoH. Not each hash by its keys, but the array by the
value of one of the keys in each hash.
I know how to sort a simple array.
I know how to sort a hash by the keys.
Could someone kindly point me to the documentation on sorting arrays of
hashes?

Thank you!
Chris.

>>>-----> <-----<<<
Chris Mortimore
Information Services
Graceland University
cmortimr at graceland dot edu

Gunnar Hjalmarsson

unread,
Aug 5, 2004, 5:27:07 PM8/5/04
to begi...@perl.org
Chris Mortimore wrote:
> I want to sort an AoH. Not each hash by its keys, but the array by
> the value of one of the keys in each hash.

The value of one of the keys? If you don't know *which* key in
respective hash, this appears to be pretty tricky...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Chris Mortimore

unread,
Aug 5, 2004, 5:43:14 PM8/5/04
to begi...@perl.org

--

Of course I know _which_ key. Each hash has a key "date_tm", I want to
sort all the hashes in the array by their date_tm value which is in the
format: yyyymmdddhhmm.

Chris.

Chris Devers

unread,
Aug 5, 2004, 5:50:51 PM8/5/04
to Chris Mortimore, begi...@perl.org
On Thu, 5 Aug 2004, Chris Mortimore wrote:

> Gunnar Hjalmarsson wrote:
> > Chris Mortimore wrote:
> >> I want to sort an AoH. Not each hash by its keys, but the array by
> >> the value of one of the keys in each hash.
> >
> > The value of one of the keys? If you don't know *which* key in
> > respective hash, this appears to be pretty tricky...
>

> Of course I know _which_ key. Each hash has a key "date_tm", I want
> to sort all the hashes in the array by their date_tm value which is in
> the format: yyyymmdddhhmm.

Don't you think it might have been constructive to mention this the
first time around?

Can you please give a fuller description of [a] what this data structure
really looks like -- a Data::Dumper dump would be good -- and [b] how
you want to rearrange the contents of this structure?


--
Chris Devers

John Moon

unread,
Aug 5, 2004, 6:10:26 PM8/5/04
to Chris Mortimore, begi...@perl.org
-----Original Message-----
From: Chris Mortimore [mailto:CMOR...@graceland.edu]
Sent: Thursday, August 05, 2004 5:19 PM
To: begi...@perl.org
Subject: Sorting an array of hashes

I want to sort an AoH. Not each hash by its keys, but the array by the
value of one of the keys in each hash.

I know how to sort a simple array.
I know how to sort a hash by the keys.
Could someone kindly point me to the documentation on sorting arrays of
hashes?

Thank you!
Chris.

Hope this gives you some ideas...


#! /usr/local/bin/perl
@AoH=({office=>C,employees=>500},{office=>A, employees=>30});
$ndx=0;
foreach (@AoH){
print "$_->{office}, $ndx \n";
$IofH{$_->{office}} = $ndx++;
}
foreach (sort keys %IofH){
print "$_, $IofH{$_}\n";
%nH = %{$AoH[$IofH{$_}]};
print "Office $_ has employees $nH{employees}\n";
}

jwm

Randy W. Sims

unread,
Aug 5, 2004, 6:23:36 PM8/5/04
to Chris Mortimore, begi...@perl.org
On 8/5/2004 5:18 PM, Chris Mortimore wrote:
> I want to sort an AoH. Not each hash by its keys, but the array by the
> value of one of the keys in each hash.
> I know how to sort a simple array.
> I know how to sort a hash by the keys.
> Could someone kindly point me to the documentation on sorting arrays of
> hashes?

`perldoc -q sort`

Ex.

my @AoH = ...
my @sorted = sort { $a->{key} cmp $b->{key} } @AoH;

Gunnar Hjalmarsson

unread,
Aug 5, 2004, 6:43:11 PM8/5/04
to begi...@perl.org
Chris Mortimore wrote:
> Gunnar Hjalmarsson wrote:
>> Chris Mortimore wrote:
>>> I want to sort an AoH. Not each hash by its keys, but the
>>> array by the value of one of the keys in each hash.
>>
>> The value of one of the keys? If you don't know *which* key in
>> respective hash, this appears to be pretty tricky...
>
> Of course I know _which_ key. Each hash has a key "date_tm", I
> want to sort all the hashes in the array by their date_tm value
> which is in the format: yyyymmdddhhmm.

Aha, the keys have the same name.. Good! Then Randy's suggested code
should do.

As regards documentation, besides "perldoc -f sort", there is a FAQ
entry that is very much applicable to this problem: "How do I sort an
array by (anything)?"

Chris Mortimore

unread,
Aug 6, 2004, 10:29:24 AM8/6/04
to Moon, John, begi...@perl.org

-----Original Message-----
From: Chris Mortimore [mailto:CMOR...@graceland.edu]
Sent: Thursday, August 05, 2004 5:19 PM
To: begi...@perl.org
Subject: Sorting an array of hashes

I want to sort an AoH. Not each hash by its keys, but the array by the

value of one of the keys in each hash. I know how to sort a simple


array. I know how to sort a hash by the keys. Could someone kindly point
me to the documentation on sorting arrays of hashes?

Thank you!
Chris.

Hope this gives you some ideas...


#! /usr/local/bin/perl @AoH=({office=>C,employees=>500},{office=>A,
employees=>30}); $ndx=0; foreach (@AoH){
print "$_->{office}, $ndx \n";
$IofH{$_->{office}} = $ndx++;
}
foreach (sort keys %IofH){
print "$_, $IofH{$_}\n";
%nH = %{$AoH[$IofH{$_}]};
print "Office $_ has employees $nH{employees}\n";
}

jwm


Thank you John. How to build the index and relate it back to the
original array of hashes is exactly what I was trying to figure out!
Gratefully, Chris.

0 new messages