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, draw).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

C++: #include<iostream>
using namespace std;
int main()
{
    int n,r;
    int wi[20];
    int lo[20];
    char c[20];
    int idx[20];
    while(cin >> n){
        for(int i=0;i<n;i++){
            cin >> c[i];
            wi[i] = 0;
            lo[i] = 0;
            idx[i] = i;
            for(int j=0;j<n-1;j++){
                cin >> r;
                if(r == 0) wi[i]++;
                if(r == 1) lo[i]++;
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n-1;j++){
                if(wi[ idx[j] ] < wi[ idx[j+1] ]) swap(idx[j],idx[j+1]);
                else if(wi[ idx[j] ] == wi[ idx[j+1] ] && lo[ idx[j] ] > lo[ idx[j+1] ])
                    swap(idx[j],idx[j+1]);
            }
        }
        for(int i=0;i<n;i++) cout << c[idx[i]] << 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;

		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;
int n, a, d[10], e[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];
			for (int j = 0; j < n - 1; j++) {
				scanf("%d", &a);
				if (a == 0) d[i]++;
				if (a == 1) e[i]++;
			}
		}
		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]);
					swap(e[i], e[j]);
				}
				else if (d[i] == d[j] && e[i] > e[j]) {
					swap(c[i], c[j]);
					swap(d[i], d[j]);
					swap(e[i], e[j]);
				}
			}
		}
		for (int i = 0; i < n; i++) printf("%c\n", c[i]);
	}
	return 0;
}

C++: #include<functional>
#include<iostream>
#include<map>
#include<utility>

typedef std::pair<int, int> P;
std::multimap<P, char, std::greater<P>> m;

int main()
{
	int n;
	while( std::cin >> n, n )
	{
		m.clear();

		for( int i = 0; i != n; ++i )
		{
			char t;
			std::cin >> t;
			int win = 0, lose = 0;
			for( int j = 0; j != n - 1; ++j )
			{
				int r;
				std::cin >> r;
				if( r != 2 )
					++( r ? lose : win );
			}

			m.insert( std::make_pair( P( win, -lose ), t ) );
		}

		for( auto it = m.begin(); it != m.end(); ++it )
			std::cout << it->second << std::endl;
	}

	return 0;
}

C++: #include<stdio.h>
int main(void)
{
	int n,p[100];
	int i,j,temp;
	int w[100],l[100],a[100];
	char t[100],m;

	scanf("%d",&n);
	while(n!=0){
		for(i=0;i<n;i++){
			w[i]=0;
			l[i]=0;
			scanf(" %c",&t[i]);
			a[i]=i;
			for(j=0;j<n-1;j++){
				scanf("%d",&p[j]);
				if(p[j]==0){
					w[i]++;
				}
				else if(p[j]==1){
					l[i]++;
				}
			}
		}
		for(i=0;i<n-1;i++){
			for(j=i+1;j<n;j++){
				if(w[i]<w[j]){
					temp=w[i];
					w[i]=w[j];
					w[j]=temp;
					m=t[i];
					t[i]=t[j];
					t[j]=m;
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				}
				else if(w[i]==w[j]){
					if(l[i]>l[j]){
						temp=l[i];
						l[i]=l[j];
						l[j]=temp;
						m=t[i];
						t[i]=t[j];
						t[j]=m;
						temp=a[i];
						a[i]=a[j];
						a[j]=temp;
					}
					else if(l[i]==l[j] && a[i]>a[j]){
						temp=w[i];
						w[i]=w[j];
						w[j]=temp;
						m=t[i];
						t[i]=t[j];
						t[j]=m;
						temp=a[i];
						a[i]=a[j];
						a[j]=temp;
					}
				}
			}
		}
		for(i=0;i<n;i++){
			printf("%c\n",t[i]);
		}
		scanf("%d",&n);
	}
	return 0;
}