foreach my $key ( map {$_->[0]} sort {$b-[1] <=> $a->[1]} map {[$_, $_ eq q{Foo}]} keys %hash; { # Do something here }