Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"sort"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Name string
	Win, Lose, Draw int
}
type lessFunc func(p1, p2 *Team) bool
type multiSorter struct {
	teams []Team
	less    []lessFunc
}
func (ms *multiSorter) Sort(teams []Team) {
	ms.teams = teams
	sort.Sort(ms)
}
func OrderedBy(less ...lessFunc) *multiSorter {
	return &multiSorter{
		less: less,
	}
}
func (ms *multiSorter) Len() int {
	return len(ms.teams)
}
func (ms *multiSorter) Swap(i, j int) {
	ms.teams[i], ms.teams[j] = ms.teams[j], ms.teams[i]
}
func (ms *multiSorter) Less(i, j int) bool {
	p, q := &ms.teams[i], &ms.teams[j]
	var k int
	for k = 0; k < len(ms.less)-1; k++ {
		less := ms.less[k]
		switch {
		case less(p, q):
			return true
		case less(q, p):
			return false
		}
	}
	return ms.less[k](p, q)
}

func main() {
	win := func(c1, c2 *Team) bool {
		return c1.Win > c2.Win
	}
	lose := func(c1, c2 *Team) bool {
		return c1.Lose < c2.Lose
	}
	draw := func(c1, c2 *Team) bool {
		return c1.Draw < c2.Draw
	}

	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		teams := make([]Team, n)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			name := buf[0]
			tmp := make([]int, n-1)
			for i := range tmp {
				tmp[i], _ = strconv.Atoi(buf[i+1])
			}
			cntwin, cntlose, cntdraw := 0, 0, 0
			for _, v := range tmp {
				switch v {
				case 1:
					cntlose++
				case 0:
					cntwin++
				case 2:
					cntdraw++
				}
			}
			teams[i] = Team{name, cntwin, cntlose, cntdraw}
		}
		OrderedBy(win, lose).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

C++: #include <iostream>
using namespace std;

struct Team {

	char name;
	int  winCnt;
	int  loseCnt;

};

int main()
{

	int n, r;
	Team t[10];

	while( cin >> n, n ) {

		for( int i = 0; i < 10; i++ ) { t[i].winCnt = 0; t[i].loseCnt = 0; }

		for( int i = 0; i < n; i++ ) {
			cin >> t[i].name;
			for( int j = 0; j < n - 1; j++ ) {

				cin >> r;
				if( r == 0 ) t[i].winCnt++;
				else if( r == 1 ) t[i].loseCnt++;

			}
		}

		for( int i = 0; i < n - 1; i++ ) {
			for( int j = i + 1; j < n; j++ ) {

				Team temp;

				if( t[i].winCnt == t[j].winCnt ) {
					if( t[i].loseCnt > t[j].loseCnt ) {

						temp = t[i];
						t[i] = t[j];
						t[j] = temp;

					}
				}
				else if( t[i].winCnt < t[j].winCnt ) {

					temp = t[i];
					t[i] = t[j];
					t[j] = temp;

				}

			}
		}

		for( int i = 0; i < n; i++ ) cout << t[i].name << endl;

	}

	return 0;

}

C++: #include<iostream>
using namespace std;
int main(){
long n,i,j,r,rr[10][3];
string s,t[10];
while(cin>>n){
if(n==0)break;
for(i=0;i<n;i++){
rr[i][0]=0;
rr[i][1]=0;
rr[i][2]=0;
}
for(i=0;i<n;i++){
cin>>t[i];
for(j=0;j<n-1;j++){cin>>r;rr[i][r]++;}
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(rr[i][0]<rr[j][0]||rr[i][0]==rr[j][0]&&rr[i][1]>rr[j][1]){
s=t[i];t[i]=t[j];t[j]=s;
r=rr[i][0];rr[i][0]=rr[j][0];rr[j][0]=r;
r=rr[i][1];rr[i][1]=rr[j][1];rr[j][1]=r;
r=rr[i][2];rr[i][2]=rr[j][2];rr[j][2]=r;
}
}
}
for(i=0;i<n;i++)cout<<t[i]<<endl;
}
return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(void){

	int n;
	char t[10];
	int r;
	int win[10] = { 0 }, lose[10] = { 0 };

	while (cin >> n && n != 0){

		for (int i = 0; i < n; i++){
			cin >> t[i];
			for (int j = 0; j < n - 1; j++){
				cin >> r;
				if (r == 0) win[i]++;
				if (r == 1) lose[i]++;
			}
		}

		for (int i = 0; i < n; i++){
			for (int j = i + 1; j < n; j++){
				if (win[i] < win[j]){
					swap(win[i], win[j]);
					swap(lose[i], lose[j]);
					swap(t[i], t[j]);
				}
				else if (win[i] == win[j]){
					if (lose[i] > lose[j]){
						swap(win[i], win[j]);
						swap(lose[i], lose[j]);
						swap(t[i], t[j]);
					}
				}
			}

			cout << t[i] << endl;
		}
	}
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int n;
	while(cin >> n && n){
		vector<string> a;
		vector< pair<int,int> > v;
		for(int i = 0 ; i < n ; i++){
			string name;
			cin >> name;
			a.push_back(name);
			int w = 0;
			for(int j = 0 ; j < n - 1 ; j++){
				int t;
				cin >> t;
				if( t == 0 ) w += 10000;
				if( t == 2 ) w += 1;
			}
			v.push_back(make_pair(-w,i));
		}
		sort(v.begin(),v.end());
		for(int i = 0 ; i < v.size() ; i++){
			cout << a[v[i].second] << endl;
		}
	}
	
}

C++: #include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;

#define _gp(l) const auto gcu{getchar##l}; const auto pcu{putchar##l}
#ifdef __linux
_gp(_unlocked);
#else
_gp();
#endif
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
_T _DEF(T,in,int c){T n=0;int m=1;while(isspace(c)){c=gcu();}if(c=='-')m=-1,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
_T _DEF(T,in,){return in<T>(gcu());}
_DEF(int,in,){return in<int>(gcu());}
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
_T _SCAN(T &n){int c=gcu();return c==EOF?n=0,false:(n=in<T>(c),true);}
_SCAN(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_SCAN(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=(char)c;}}
#endif
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);} _OUT(const char *s){while(*s)pcu(*s++);} _OUT(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_OUT(string s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _OUT(vector<T> v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
_HT _OUT(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _OUTL(T&&... t){out(move(t)...);outl();}
struct range{
	int e,b=0,s=1; range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
	struct it { int v, s; it (int _v, int _s) : v(_v), s(_s) {} operator int()const{return v;} operator int&(){return v;} int operator*()const{return v;}
		it& operator++(){v+=s;return *this;} }; it begin() {return {b, s};} it end() {return {e, s};}};

struct T {
	int w, l;
	char t;
	bool operator < (T a) const {
		return w == a.w ? l < a.l : w > a.w;
	}
};

int main() {
	for (int n; (n = in());) {
		vector<T> s(n);
		for (T &i: s) {
			i.t = gcu();
			gcu();
			for (int j {}, e {n - 1}; j < e; j++)
				switch (in()) {
				case 0:
					i.w++;
					break;
				case 1:
					i.l++;
				}
		}
		stable_sort(begin(s), end(s));
		for (T i: s)
			outl(i.t);
	}
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
pair<int, string>a[100000];
int n, sum, p;
int main() {
	while (true) {
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> a[i].second;
			sum = 0;
			for (int j = 0; j < n - 1; j++) {
				cin >> p;
				if (p == 0) { sum += 10000; }
				if (p == 2) { sum += 1; }
			}
			a[i].first = sum;
		}
		sort(a, a + n);
		for (int i = n - 1; i >= 0; i--) {
			cout << a[i].second << endl;
		}
	}
}