The way we tend to do this is as follows :-
1) Allocate the hits to the different units front to back, and then repeat until all hits are allocated (so the units at the front will have one more hit than those at the back). Think of this like adding 'layers' of hits to the formation.
2) For each class or type of unit hit, throw a 'layer' of hits simultaneously, removing surplus dice for units that have died from succeeding 'layers'. Assume that all kills are removed from front to back in the relevant groups.
An example might make this a little clearer:-
4x Devastators get 10x hits. Being clever, the marine player has put the Librarian at the back, so notionally, the two lead Devs get 3x hits, while the Librarian and the other dev only get 2x hits.- Normal Devs 1st 'layer':-
throw 2x dice for the lead Devs only (as they have more hits) - let's assume one dies
- Normal Devs 2nd layer:-
(include the guy at the back this time, but deduct the dice for the dead man), so throw 2x dice and again lets assume one dies.
- Normal Devs 3rd 'layer':-
throw 1x Dice for the survivor at the back - and he survives!!
- Now for the Librarian - well let's just throw his two hits together, and if any hits are scored, throw an invulnerable save for each.
We adopt a similar approach for normal hits on RA formations; throwing all the dice for hits on a single group of units, but rerolling those dice that were not saved to see how many kills were scored.
Regarding mixed types of hit, any MW and TK hits on an unarmoured target just kills the unit so any 'normal' hits can be ignored; while on RA targets, you roll the normal hits first, and then add the MW hits to any rerolls required.