In fact, I don't think you need a loop at all
give the sprites another value called something like 'Team'
Add another parameter (after the other one) to each of the functions, and set it to 'Team'
Then make the functions:
+sprite value 'Team'=Function.Param(2)
+sprite value 'Follow'<Function.Param(1)
+pick with highest value 'Follow'
That should work, then only the ones in their own team will follow eachother, and they'll follow the next highest value
so if red team 3,6,8,9 and blue team 1,2,4,5,7,.
9 follows 8 follows 6 follows 3 which doesn't follow anything (so you'd set it to)
7 follows 5 etc