简体   繁体   中英

Perl: Sorting hash of hash by value descending order

data :

%HoH => (
 abc => {
           value    => "12",
 },
 xyz => {
           number    => "100",
 },
 pqr => {
           digit    => "5",
 }
)

How do I sort the hash of hash by value in descending order? Output

100
12
5

You can't sort a hash, it won't hold the order . If you wanted to keep them sorted, you'll have to sort the keys based on the number and store the keys in an array .

#!/usr/bin/perl
use strict;
use warnings;

my %HoH = (
                abc => { value => 12 },
                xyz => { value => 100},
                pqr => { value => 5},
                def => { value => 15},
                hij => { value => 30},
          );

my @sorted_keys = map  { $_->[0] }
          sort { $b->[1] <=> $a->[1] } # use numeric comparison
          map  { my $temp;
                if ( exists $HoH{$_}{'value'} ) {
                        $temp = $HoH{$_}{'value'};
                } elsif ( exists $HoH{$_}{'number'} ) {
                        $temp = $HoH{$_}{'number'};
                } elsif ( exists $HoH{$_}{'digit'} ) {
                        $temp = $HoH{$_}{'digit'};
                } else {
                        $temp = 0;
                }
                {[$_, $temp]} }
               (keys %HoH);

for my $key (@sorted_keys) {
        my $temp;
        if ( exists $HoH{$key}{'value'} ) {
                $temp = $HoH{$key}{'value'};
        } elsif ( exists $HoH{$key}{'number'} ) {
                $temp = $HoH{$key}{'number'};
        } elsif ( exists $HoH{$key}{'digit'} ) {
                $temp = $HoH{$key}{'digit'};
        } else {
                $temp = 0;
        }
        print $key . ":" . $temp ."\n";
}

Output:

xyz:100
hij:30
def:15
abc:12
pqr:5

This technique to do the sorting is called Schwartzian Transform .

I came up with this solution

#!/usr/bin/perl

use strict;
use warnings;

my %HoH = (
     abc => {
               value    => "12",
     },
     xyz => {
               number    => "100",
     },
     pqr => {
               digit    => "5",
     }
    );

my %rever;  
for my $TopKey(keys %HoH){
    for my $value(values %{ $HoH{$TopKey} }){
        push @{ $rever{$value} }, $TopKey;
    }
}

my @nums = sort {$b <=> $a} (keys(%rever));

print $_, "\n" for @nums;

I reversed the values in case you still needed to use the key names.

This is how it looks after using Dumper.

$VAR1 = '100';
$VAR2 = [
          'xyz'
        ];
$VAR3 = '12';
$VAR4 = [
          'abc'
        ];
$VAR5 = '5';
$VAR6 = [
          'pqr'
        ];

Given you're not actually using the keys for anything, you can flatten the data structure into a single array and then sort it:

use strict;
use warnings;

my %HoH = (
    abc => {value    => "12",},
    xyz => {number    => "100",},
    pqr => {digit    => "5",},
);

my @numbers = sort {$b <=> $a} map {values %$_} values %HoH;

print "$_\n" for @numbers;

Outputs:

100
12
5

However, if you want to use the additional key information, then you'll need fold your Hash of Hash into an array, and then you can sort however you like:

my @array;
while (my ($k, $ref) = each %HoH) {
    while (my ($k2, $v) = each %$ref) {
        push @array, [$k, $k2, $v];
    }
}

@array = sort {$b->[2] <=> $a->[2]} @array;

use Data::Dump;
dd \@array;

Outputs:

[
  ["xyz", "number", 100],
  ["abc", "value", 12],
  ["pqr", "digit", 5],
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM