Re: Sorting array of hash references



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
.



Relevant Pages

  • Re: Finding Login Hours for user accounts in AD
    ... The logonHours attribute is a byte array. ... Accounts with logon hours set will display in the format; ... created by copying other user accounts and retain the same properlys. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Finding Login Hours for user accounts in AD
    ... The logonHours attribute is a byte array. ... Accounts with logon hours set will display in the format; ... created by copying other user accounts and retain the same properlys. ...
    (microsoft.public.windows.server.active_directory)
  • Re: Elementary but surprisingly difficult.
    ... with all manner of notions of array order. ... "abcbdecadec" Eliminate duplicates but preserve the original order? ... about whether an order preserving reduction can be done and which of the ...
    (comp.lang.forth)
  • Re: Array and InputBox Integration ?
    ... I am having a little trouble getting values of an array to number ... themselves in an InputBox for a user to make their choice. ... I am pulling all the local accounts off the machine which will vary ... Sub ListLocalAccounts ...
    (microsoft.public.scripting.wsh)
  • Re: Vlookup with Multiple Criteria
    ... will return an array of true/falses--depending on if A2 is equal ... I have 2 spreadsheets that both contain store numbers and the accounts they ... I usually use the VLOOKUP formula to search for the store numbers in the ...
    (microsoft.public.excel.newusers)