Reason why it happens is because items and stacks display in order that they were added into container, but when you take first item from the stack, stack moves to position of next item in the stack. Easiest solution here would be to display order by last item in the stack, this will minimize stacks movement because you always take first item in the stack and order would never change.