# Sorting an array of hashes - PERL Beginners

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 doentation on sorting arrays of hashes? Thank you! Chris.  [/ref][/ref] Chris Mortimore Information Services Graceland University cmortimr at graceland dot edu...

1. ## 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 doentation on sorting arrays of
hashes?

Thank you!
Chris.
[/ref][/ref]
Chris Mortimore
Information Services
Graceland University
cmortimr at graceland dot edu

Chris Guest

2. ## Re: Sorting an array of hashes

Chris Mortimore wrote:

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

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

3. ## RE: Sorting an array of hashes

Chris Mortimore wrote:

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

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

--

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 Guest

4. ## RE: Sorting an array of hashes

On Thu, 5 Aug 2004, Chris Mortimore wrote:

> >
> > The value of one of the keys? If you don't know *which* key in
> > respective hash, this appears to be pretty tricky...[/ref]
>
> 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.[/ref]

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
Chris Guest

5. ## RE: Sorting an array of hashes

-----Original Message-----
From: Chris Mortimore [mailto:edu]
Sent: Thursday, August 05, 2004 5:19 PM
To: 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 doentation 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
John Guest

6. ## Re: Sorting an array of hashes

On 8/5/2004 5:18 PM, Chris Mortimore wrote:

`perldoc -q sort`

Ex.

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

Randy Guest

7. ## Re: Sorting an array of hashes

Chris Mortimore wrote:
>>
>> The value of one of the keys? If you don't know *which* key in
>> respective hash, this appears to be pretty tricky...[/ref]
>
> 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.[/ref]

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

As regards doentation, 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)?"

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

8. ## RE: Sorting an array of hashes

-----Original Message-----
From: Chris Mortimore [mailto:edu]
Sent: Thursday, August 05, 2004 5:19 PM
To: 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 doentation 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.
Chris Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•