Yeah I hate programming and scripting in general. However it seems like a pretty simple logical solution, try to balance by skill (aka either score, or K:D and go back either 1 round, 5 rounds, 1 map, 5 maps, 1 day, 5 days, lifetime, etc..you choose), then try to balance by class (doesn't need to be 50:50, would be better if it was in the area of 30:70 or more balanced), and then put people on same banner on same team if it doesn't interrupt the previous two conditions.
I'm guessing 90% of the time people of the same banner would still be on the same team.
I'm not saying I have the solution, but I certainly recognize an issue that has existed for a long time. Teams have always had a problem being balanced even close to equally (in relation to both skill and class balance).