Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Perl
Perl Misc
Sorting AofH over hash key(s)...
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Salvador Fandino, post: 4846126"] You can use Sort::Key or Sort::Maker from CPAN to do that: For instance, to sort by color then by width: use Sort::Key::Multi qw(si_keysort); # si_ stands for string + integer # is like saying use 'cmp' to compare the first key # and '<=>' for the second my @sorted = si_keysort { $_->{color}, $_->{width} } @$h; You can even generate the sorter function dynamically: use Sort::Key qw(multikeysorter); # define how the different data properties have to be compared: my %sorting_types = (color => 'string', size => 'integer', width => 'integer', height => 'integer'); my @order = qw(color size width); # change to suit your needs; my @sorting_types = map $sorting_types{$_}, @order; my $sorter = multikeysorter(sub { @{$_}{@order} # that's a hash slice... }, @sorting_types); my $sorted_data = $sorter->(@data); Cheers, - Salva [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Perl
Perl Misc
Sorting AofH over hash key(s)...
Top