Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: need to sort array of hash

by BillKSmith (Monsignor)
on Dec 22, 2018 at 15:28 UTC ( [id://1227610]=note: print w/replies, xml ) Need Help??


in reply to need to sort array of hash

I agree with the other monks that you probably should use a better data structure. However, you can use the Schwartzian Transform described in the FAQ to solve the original problem.
$type 1227601.pl use strict; use warnings; use Data::Dumper; my $ArrayofHash = [ { 'TaskA' => { 'maruti' => '20', 'honda' => '25', 'zen' => '25', 'hyundai' => '35', 'ford' => '22', 'toyota' => '11' } }, { 'TaskB' => { 'maruti' => '11', 'honda' => '22', 'zen' => '33', 'hyundai' => '33', 'ford' => '24', 'toyota' => '16' } }, { 'TaskC' => { 'maruti' => '12', 'honda' => '22', 'zen' => '33', 'hyundai' => '44', 'ford' => '55', 'toyota' => '66' } } ]; my $sorted = [ map {$_->[1]} sort {$b->[0] <=> $a->[0]} #map {(my $h) = values %$_; [$h->{hyundai}, $_]} map {[(values %$_)[0]{hyundai}, $_]} @$ArrayofHash ]; print Dumper($sorted); $perl 1227601.pl $VAR1 = [ { 'TaskC' => { 'zen' => '33', 'toyota' => '66', 'maruti' => '12', 'hyundai' => '44', 'honda' => '22', 'ford' => '55' } }, { 'TaskA' => { 'ford' => '22', 'toyota' => '11', 'maruti' => '20', 'hyundai' => '35', 'honda' => '25', 'zen' => '25' } }, { 'TaskB' => { 'ford' => '24', 'honda' => '22', 'hyundai' => '33', 'maruti' => '11', 'toyota' => '16', 'zen' => '33' } } ]; $

UPDATE: Modified code to simplify dereference. Note: Using the first (and only) element of values avoids the problem with not knowing the name of the key.

Bill

Replies are listed 'Best First'.
Re^2: need to sort array of hash
by dsheroh (Monsignor) on Dec 23, 2018 at 11:54 UTC
    Note: Using the first (and only) element of values avoids the problem with not knowing the name of the key.
    ...but that only applies to the given sample data because the intermediate-level hash only contains a single key/value pair. If there are multiple keys/values in those hashes, you're screwed because values will return the values in a random order. (But the order is stable within a single run of the program, so long as the hash isn't modified, so at least it shouldn't throw sort into an infinite loop. You'll just potentially get results sorted on the wrong sub-hash.)
      If there were more key/value pairs in the intermediate-level hash, we would need additional specifications to tell us which one to use in the sort. You have provided a detailed explanation of why this design could not meet such a requirement. (I am not even sure it could be modified to do so.) Even if the recommended redesign of the data structure could not be justified for current structure, one more requirement should surely tip the balance.
      Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found