Sort the list of instances before assigning ids