http://qs321.pair.com?node_id=1212370


in reply to Re^2: Sorting hash
in thread Sorting hash

Hi,

there you go.

i must confess that i missed also 1 thing in your sort function.

the cpu percentage will not interpreted as an numeric value, so you have to use cmp instead of <=>.

see perlop

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %results = ( 'hostname1' => { '1' => { 'pass' => 1, 'cpu' => '0.07%', 'box_name' => 'hostname', 'capacity' => '0.41%' } }, 'hostname2' => { '1' => { 'pass' => 1, 'cpu' => '0.04%', 'box_name' => 'hostname', 'capacity' => '0.25%' } } ); my $results = \%results; foreach my $router ( sort { $results->{$a}{1}{cpu} cmp $results->{$b}{ +1}{cpu} } keys %{results} ) { print Dumper($results->{$router}); }