The order that values are entered into a hash are not relevant to how they are later retrieved. You cannot count on the order of hash keys or values. Your best bet is probably to sort on the keys before iterating through them.