in reply to something weird in this hash sorting

Put use Data::Dumper; at the top of your script and, after you have created your %name hash, put these lines

print Data::Dumper->Dump( [ \ %name ], [ qw{ *name } ] ); exit;

and see whether the contents of the hash are what you expected. Another thing, your hash is, apparently, %name but you sort keys (%final). Some discrepancy there!

I hope this helps you.