Sorting an array of hashes by 2 values

P

Paul Tomlinson

Hello, I have an array of variable size which contains a hash with three
keys: count, name, description. I have a call to sort which sorts this
array by count, but I also want this call to sort by name within count.

This is my call:

@Stats = sort {$b->{Program}{Count} <=> $a->{Program}{Count}} @Stats;

which like I say sorts by count (descending), can anyone help me in sorting
by name within count please.

Paul.
 
P

Paul Tomlinson

Thanks Bernard that kinda worked, however in the sort capital "X" comes
BEFORE lowercase "a" which is not as I desire, is there anyway to get around
this perculiarity? and have it listed as "A" ... "a" ... "B" ... "b" ....
"X" ... "x"

Your thoughts?
 
B

Benjamin Goldberg

Paul Tomlinson wrote:
[snip]
Thanks Bernard that kinda worked, however in the sort capital "X" comes
BEFORE lowercase "a" which is not as I desire, is there anyway to get around
this perculiarity? and have it listed as "A" ... "a" ... "B" ... "b" ....
"X" ... "x"

@Stats = sort {$b->{Program}{Count} <=> $a->{Program}{Count} ||
uc($a->{Program}{Name}) cmp uc($b->{Program}{Name}) ||
$a->{Program}{Name} cmp $b->{Program}{Name}} @Stats;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top