Ongoing Each Player: Create List by Index

Straight to the point: I want to make a list that sorts players’ depending on their team, if they are marked by a player variable they will be the first person at the beginning of their team in the list.

I have made a workshop script that divides players into 3 teams. Then each team gets to pick 0 or 1 twice, once for each player (but not more than a total of 2 votes in that team, one voter is marked by variable as O = 1). I create a temporary list with player names H. Then after this, I want to make a list that sorts the choices of each player (0 or 1) in order of team and O = 1 players will be first in that list for that team.

Example (I’ll use teams 1 and 2 instead of variables for simplicity):

Team 1:
X (chooses 1) - O = 1
Y (chooses 0)

Team 2:
A (chooses 1)
B (chooses 0) - O = 1
C (empty)

H list:
X
Y
B
A
C

The list I want to create should be like:
1
0
0
1
(empty)


What I have done is to create a Ongoing Each Player script that looks like this:

O = Random selected ally in a team who gains the second choice out of the two.
V (global) = New list with values of choices in order of team and value of O.
V (player) = Player’s choice
Team = (actually a variable but I simplified going with team 1 and 2 in this post).
H = List of player names.

“”"
_If Player Variable of Event Player O != 1: Skip (2)
If Team of Event player != 1: Skip (1)
Modify Global Variable H append to array Event Player.
Wait 0.5 sec
If Player Variable of Event Player O = 1: Skip (2)
If Team of Event player != 1: Skip (1)
Modify Global Variable H append to array Event Player.
Wait 0.5 sec
(…) #Same again but Team 2 instead of Team 1

Set Global Variable V at Index (index of global variable H with value: event player) to Value: Player Variable V of event player._
“”"

The problem with this code is that the teams are sometimes jumbled up and 2 comes before 1 and O = 0 comes before players with O = 1 in list V.

My question: How can I make a list or function where I keep track of both team, choice and if the player had O = 1 or not.