Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^2: sorting hash of array of hashes by value

by LanX (Saint)
on Aug 26, 2014 at 21:25 UTC ( [id://1098681]=note: print w/replies, xml ) Need Help??


in reply to Re: sorting hash of array of hashes by value
in thread sorting hash of array of hashes by value

Here a proof of concept: (stealing data from AppleFritter's example)

#!/usr/bin/perl use strict; use warnings; use feature qw/say/; use Data::Dump qw/pp dd/; my %data = ( "key0.1" => [ { "key1.1" => 3.3, "key1.2" => 17.8, "key1.3" => -2.4, }, { "key1.4" => 5.1, "key1.5" => 13, }, { "key1.6" => -69, "key1.7" => 127, "key1.8" => 2.718, "key1.9" => 3.3, }, ], "key0.2" => [ { "key1.10" => 3.3, "key1.11" => 2.5, }, { "key1.12" => -33, }, ], ); my %flat = (); while (my ($k0,$v0) = each %data) { my $k1=-1; for my $v1 (@$v0) { $k1++; while (my ($k2,$v2) = each %$v1) { $flat{$k0,$k1,$k2} = $v2; } } } my @sorted_keys = sort { $flat{$a} <=> $flat{$b} } keys %flat; for my $k (@sorted_keys) { my $v = $flat{$k}; say "$v \t<= \t", join ", ", split ( $; , $k ); }

Output:

-69 <= key0.1, 2, key1.6 -33 <= key0.2, 1, key1.12 -2.4 <= key0.1, 0, key1.3 2.5 <= key0.2, 0, key1.11 2.718 <= key0.1, 2, key1.8 3.3 <= key0.1, 0, key1.1 3.3 <= key0.1, 2, key1.9 3.3 <= key0.2, 0, key1.10 5.1 <= key0.1, 1, key1.4 13 <= key0.1, 1, key1.5 17.8 <= key0.1, 0, key1.2 127 <= key0.1, 2, key1.7

HTH! =)

Cheers Rolf

(addicted to the Perl Programming Language and ☆☆☆☆ :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-25 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found