Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Sorting an array or hashes

by salva (Canon)
on Jan 23, 2012 at 10:07 UTC ( [id://949349]=note: print w/replies, xml ) Need Help??


in reply to Sorting an array or hashes

Those so similar and ugly sorting blocks can be generated from metadata, specially if you use some sorting module from CPAN as Sort::Key or Sort::Maker:
# untested! use Sort::Key; my %key_type = (CollectionId => 'int', Framecount => 'int', Status => 'str', Missing => 'int', Modified => 'str'); my %order = (collection => [qw(-CollectionId -Modified)], # the minus framecount => [qw(-Framecount -Modified)], # sign means status => [qw(Status -Modified)], # descending + order missing => [qw(-Missing -Modified)], modified => [qw(-Modified)]); my %sorter; for my $order (keys %order) { my @types; my @keys; for (@{$order{$order}}) { /^(-?)(\w+)$/ or die; push @types, "$1$key_type{$2}"; push @keys, $2; } $sorter{$order} = Sort::Key::multikeysorter { @{$_}{@keys} } @types; } sub printCollectionData { ... # Sort the collection according to orderBy param @sortedCollectionData = $sorter{$orderBy}->(@collectionData); ... }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://949349]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2025-12-12 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (92 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.