#! /usr/bin/perl use strict; use warnings; my %h; # if your data is really numerical then the apostrophes are superfluous $h{10}{20} = 5; $h{10}{25} = 4; $h{05}{50} = 3; $h{20}{30} = 2; my @temp; for my $key1 ( keys %h ) { for my $key2 ( keys %{ $h{$key1} } ) { # create a 3-column table from your hash push @temp, [ $key1, $key2, $h{$key1}{$key2} ]; } } # sort it by the second column numerically for ( sort { $a->[1] <=> $b->[1] } @temp ) { print 'key1=', $_->[0], ', key2=', $_->[1], ', value=', $_->[2], "\n"; }