timecatalyst has asked for the wisdom of the Perl Monks concerning the following question:
As you can see, if I simply call reverse on the list, it will also reverse the name field order, which I need to retain. I was wondering if there was some kind of magic I am overlooking to get the proper results.my @hashes = ( {name=>'a', flag=>1}, {name=>'a', flag=>0}, {name=>'b', flag=>1}, {name=>'c', flag=>0}, {name=>'b', flag=>0} ); @hashes = sort { $a->{name} cmp $b->{name} or $a->{flag} cmp $b->{flag} } @h +ashes; The order that this gives me is: name: a flag: 0 name: b flag: 0 name: c flag: 0 name: a flag: 1 name: b flag: 1 But what I want is: name: a flag: 1 name: b flag: 1 name: a flag: 0 name: b flag: 0 name: c flag: 0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Sort hash values with one key in reverse
by Limbic~Region (Chancellor) on Apr 11, 2006 at 16:41 UTC | |
by timecatalyst (Acolyte) on Apr 11, 2006 at 16:55 UTC | |
|
Re: Sort hash values with one key in reverse
by salva (Canon) on Apr 11, 2006 at 16:53 UTC |