This seems to be a unique problem, but forgive me if it was addressed before.
I'm trying to perform a hash sort by 2 values, but the stipulation is I need 1 of those values to be reverse sorted.
Here's what I have (excuse my verbosity, I wanted to be very explicit):
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
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.
Thanks in advance,
Andy.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.