It depends which one you select in the new formation when attatching it. has always been a bit random. I think I usually point the new element at the last one in the new formation, and not the leader element.
It's why the "fix fromation leader" function was added to the HQ menu in deployment phase. As well it also sorts out when the HQ gets shifted in a LC due to the leader getting dead.
It quite often sorts them in buy order.
Its best to assign a unit to a formation that was bought previously to maintain the order.
So from D > C
If you do it the other way it sometimes shuffles them along.