Right complicated Vesku is correct you need to buy in order you want & remember cant reassign the current leader.
Easy way to explain
units keep there buying order so.
Any units added after will go on the end.
Any units bought before will go on the front taking over the 0 slot & shuffling all others back one.
I dont think this however changes the leader so not advised.
In summary buy the formation you want
Buy the units you want to add in the order you want them added
Add them & delete any unwanted units.
I do it on occasion for campaigns.