Updated foreach of hash keys to include sort.
Updated tests.