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
	}

	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 <bits/stdc++.h>
using namespace std;

int main() {
  for(int n; cin >> n && n;) {
    vector<pair<pair<int,int>,pair<int,char> > > v;
    for(int i = 0; i < n; ++i) {
      char t;
      cin >> t;
      int a = 0, b = 0;
      for(int j = 0; j < n-1; ++j) {
        int r;
        cin >> r;
        if(r == 0) ++a;
        else if(r == 1) ++b;
      }
      v.push_back(make_pair(make_pair(-a, b), make_pair(i, t)));
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < v.size(); ++i) {
      cout << v[i].second.second << endl;
    }
  }
  return 0;
}

C++: #include<stdio.h>
int main(void)
{
    int n,i,j,a[10][9],b[10],w,flg[10];
	char name[10],x;
	while(1){
	    scanf("%d",&n);
		if(n==0) break;
		for(i=0;i<n;i++){
		    scanf(" %c",&name[i]);
			for(j=0;j<n-1;j++){
			    scanf(" %d",&a[i][j]);
			}
		}
		for(i=0;i<n;i++) {
		    b[i]=0;
			flg[i]=i;
		}
		for(i=0;i<n;i++){
		    for(j=0;j<n-1;j++){
			    if(a[i][j]==0) b[i]=b[i]+10;
				else if(a[i][j]==2) b[i]=b[i]+1;
			}
		}	
		for(i=0;i<n-1;i++){
		    for(j=i+1;j<n;j++){
			    if(b[i]<b[j]){
				    w=b[i];
					b[i]=b[j];
					b[j]=w;
					x=name[i];
					name[i]=name[j];
					name[j]=x;
					w=flg[i];
					flg[i]=flg[j];
					flg[j]=w;
				}
			}
		}
		for(i=0;i<n-1;i++){
		    if(b[i]==b[i+1]&&flg[i]>flg[i+1]){
			    w=b[i];
				b[i]=b[i+1];
				b[i+1]=w;
				x=name[i];
				name[i]=name[i+1];
				name[i+1]=x;
				w=flg[i];
				flg[i]=flg[i+1];
				flg[i+1]=w;
			}
		}
		for(i=0;i<n;i++) printf("%c\n",name[i]);
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

int main()
{
	int n;
	while (scanf("%d", &n), n)
	{
		pair<int, char> scores[10];
		for (int i = 0; i < n; ++i)
		{
			char name;
			scanf(" %c", &name);
			int w, l;
			w = l = 0;
			for (int j = 1; j < n; ++j)
			{
				int t;
				scanf("%d", &t);
				if (t == 0)
					++w;
				else if (t == 1)
					++l;
			}
			scores[i] = make_pair(w*1000 - l, name);
		}
		sort(scores, scores+n, greater<pair<int, char> >());
		for (int i = 0; i < n; ++i)
			printf("%c\n", scores[i].second);
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, a, d[10]; char c[10];
int main() {
	while (true) {
		scanf("%d", &n);
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			cin >> c[i]; d[i] = 0;
			for (int j = 0; j < n - 1; j++) {
				scanf("%d", &a);
				if (a == 0) d[i] += 10;
				if (a == 1) d[i] -= 1;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (d[i] < d[j]) {
					swap(c[i], c[j]);
					swap(d[i], d[j]);
				}
			}
		}
		for (int i = 0; i < n; i++) printf("%c\n", c[i]);
	}
	return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

vector<pair<int,string> > team(20);

int main() {
	int n;
	int score;
	while(n=in()){
		int i;
		for(i=0;i<n;i++){
			team[i].first=0;
			cin>>team[i].second;
//cout<<team[i].second<<endl;
			int j;
			for(j=0;j<n-1;j++){
				score=in();
				if(score==0)team[i].first+=100;
				if(score==2)team[i].first++;
//cout<<" "<<score<<endl;
			}
		}
		stable_sort(&team[0],&team[n],greater<pair<int,string> >());
		for(i=0;i<n;i++){
			cout<<team[i].second<<endl;
		}
	}
	return 0;
}

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

class team
{
	public:

		team(){c=w=l=0;}

		char c;
		int w,l;

		bool operator<(const team& t) const
		{
			return w!=t.w ? w>t.w : l<t.l;
		}
};

int main()
{
	int n,s;
	while(cin >> n, n)
	{
		vector<team> v;

		for(int j=0; j<n; j++)
		{
			team t;
			cin >> t.c;

			for(int i=0; i<n-1; i++)
			{
				cin >> s;
				if(s==0) { t.w++; }
				if(s==1) { t.l++; }
			}
			v.push_back(t);
		}
		stable_sort(v.begin(), v.end());
		for(int i=0; i<v.size(); i++)
		{
			cout << v[i].c << endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef pair<pair<int, int>, char> p;
int main() {
	int n;
	while (cin >> n, n) {
		p a[20];
		for (int i = 0; i < n; i++) {
			cin >> a[i].second;
			a[i].first.first = 0;
			a[i].first.second = n;
			for (int j = 1; j < n; j++) {
				int k; cin >> k;
				if (!k)a[i].first.first++;
				else if (k == 1)a[i].first.second--;
			}
		}
		for (int i = n; i >= 1; i--)
			for (int j = 1; j < i; j++)
				if (a[j - 1].first < a[j].first)swap(a[j - 1], a[j]);
		for (int i = 0; i < n; i++)
			cout << a[i].second << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>

using namespace std;

#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)

class state{
public :
	char t;
	int w,l,id;
	bool operator<(const state & a)const{
		if (w!=a.w) return w > a.w;
		if (l!=a.l) return l < a.l;
		return id<a.id;
	}	
};

main(){
	int n;
	while(cin>>n,n){
		state data[n];
		rep(i,n){
			cin>>data[i].t;
			data[i].id=i;
			data[i].w =0;
			data[i].l =0;
			rep(j,n-1){
				int tmp;
				cin>>tmp;
				if(tmp==0)data[i].w++;
				else if(tmp==1)data[i].l++;
			}
		}
		sort(data,data+n);
		rep(i,n)cout<<data[i].t<<endl;
	}
}