You can use the
Tie::Hash::Sorted module. This module presents hashes in sorted order.
Alternatively, you can also use the
Tie::IxHash module and add the elements into the hash in ascending order of $a. Tie::IxHash module implements hashes that preserve the order in which the hash elements were added into the hash.