foreach my $key (sort { $patients->{$a}->{old_file} cmp $patients->{$b}->{old_file} } (keys %{$patients})) { # print ... }