As already outlined, you allocate the hits to units and later you test for saves. The "quick" method is:-
- Allocate the hits from front-to-back,
- Save groups of units with the same factors
- Then remove the casualties from front-to-back.
This eventually leaves a gap between the opposing formations where the leading units have been removed. However it can produce some anomalies like the one you are highlighting.
Where the 'detail' matters, we allocate hits by placing small dice (or counters) against the relevant units, then resolving each unit in turn (so throwing the dice and removing the unit if it dies). This way there is no dispute over the order that units are removed.