Clean up duplication of hash_array sorting code.