RPG Battle Order

I am trying to develop a battle system order engine which determines the order of turns according to player(s) and enemy(s) speed. For example, let’s say that player A has a speed of 4 and player B has a speed of 2. The order of battle would be like this: Player A, Player A, Player B, Player A, Player A, Player B, Player B, etc. Since Player A has twice as much speed has Player B, player A goes twice for every turn that Player B makes. Does this make since?

I have absolutly no clue how to do this (though I am experienced in AS, I haven’t ever came across this kind of scenario - so I have absolutly NO clue where/how to begin and debug it. If isn’t to much to ask, can someone provide a small snippet of code for me. Please?

Thanks in advance.

*Also, if it isn’t too much to ask, can you make flexible enough so that there would be more players in battle (Player C with a speed of 1, Player D with a speed of 5)? This isn’t a biggy right now, but would be very nice. Thanks