Re: Sorting array of hash references
- From: anno4000@xxxxxxxxxxxxxxxxxxxxxx
- Date: 26 Oct 2006 10:38:52 GMT
alwaysonnet <kalyanrajsista@xxxxxxxxx> wrote in comp.lang.perl.misc:
hi all,
I've got an array of hash-references as following. ( i've represented
hash-references as a table below)
It would have been *much* better to present Perl code that produces
the array of hashes instead of a table that leaves us guessing how
your data relate to your code.
Type A/C No Status(active/inactive)
Prefund 12345 Y
Prefund 45678 N
Receipt 78878 N
Receipt 32365 Y
Payment 56546 N
Payment 23456 N
Payment 34093 Y
I want to display all the active accounts (status with "Y") before
inactive accounts along with preserving the order by type ie., Prefund
should come first before Receipt and Payment along the order.
Unless you're running a rather old version of Perl, Perl's sort() is
stable, meaning the original order is preserved when elements compare
equal. If you simply sort by status the result should be what you
want.
I've got the code which i've hard-coded the account type(Payment) and
then sorting according to their status.
foreach my $label(@accts) {
if ($label->{'batype'} eq "Payment") {
Is "batype" what appears in the column "Type"? Is "baactive" what
appears in column "Status(active/inactive)"? Please make explicit
what is what, don't let your readers guess.
push(@acct_pay,$label);
}
}
foreach $x(@acct_pay) {
@sorted = sort { $b->{'baactive'} cmp $a->{'baactive'} } @acct_pay;
}
Is there any way of how to achieve this by sorting according to status
by preserving the account type order.
That will only show the three "Payment" records. What does it have to
do with your question?
Anno
.
- Follow-Ups:
- Re: Sorting array of hash references
- From: alwaysonnet
- Re: Sorting array of hash references
- References:
- Sorting array of hash references
- From: alwaysonnet
- Sorting array of hash references
- Prev by Date: Sorting array of hash references
- Next by Date: Re: Remove short words from a string
- Previous by thread: Sorting array of hash references
- Next by thread: Re: Sorting array of hash references
- Index(es):
Relevant Pages
|