my @flat_data; for my $file (keys %hoh){ for (values %{$hoh{$file}}){ push @flat_data, [ @{$_}{'weight', 'intensity'}, $file ]; } } printf "weight: %d intensity %d Filename: %s\n", @$_ for sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] || $a->[2] cmp $b->[2] } @flat_data;