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>
#include<vector>
#include<string>
#include<utility>
#include<algorithm>
using namespace std;

bool hikaku(const pair<int,string>& A,const pair<int,string>& B){
	return A.first > B.first;
}
int main(){
	int n,k,win,lose,les;
	string s;
	
	while(cin >> n,n){
		vector<pair<int,string> >p; // vector < pair <string, int> >p;
	
		for(int i=0;i<n;i++){
			cin >> s ;// cin >> s;
			les = 0;
			for(int j=0;j<n-1;j++){
				cin >> k;
				if(k==0)
				les+=10;//les++;
				if(k==2)//if(k==1)
				les+=1;
			}
			p.push_back( make_pair(les,s) );
		}
		stable_sort(p.begin(),p.end(),hikaku );
		//stable_sprt(p.begin(),p.end(),greater<pair<string , int> >);
		for(int i=0;i<n;i++)c
		cout << p[i].second<<endl;
		
	}
	
	
}

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

C++: #include<iostream>
#include<cstdio>
#include <cstring>
#include<map>
#include<string>
#include<stack>
#include <algorithm>
#include <sstream>
#include<vector>
using namespace std;


int main(void){
	multimap<int,char,greater<int> > team;
	char t;
	int n;

	while(1)
	{
		cin>>n;
		if(!n) break;
		
		for(int j=0; j<n; j++)
		{
			cin>>t;
			int point=0,r;
			
			for(int i=0; i<n-1; i++)
			{
				cin>>r;
				if(r==0) point+=2;
				else if(r==2) point++;
			}
			team.insert( make_pair( point,t) );
		}

		for(multimap<int,char,greater<int> >::iterator it=team.begin(); it != team.end(); ++it)
			cout<<(*it).second<<endl;
		team.clear();
	}
	return 0;
}

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

struct team {
    char name;
    int win, lose;
};

void mysort(team t[], int n) {
    for (int i = 0; i < n; i++) {
        for (int j = i; j > 0; j--) {
            if (t[j - 1].win < t[j].win || t[j - 1].win == t[j].win 
                && t[j - 1].lose > t[j].lose) {
                team temp = t[j - 1];
                t[j - 1] = t[j];
                t[j] = temp;
            }
        }
    }
}

int main() {
    while (true) {
        int n;
        cin >> n;
        if (!n) return 0;
        
        team t[n];
        for (int i = 0; i < n; i++) {
            t[i].win = 0;
            t[i].lose = 0;
        }

        for (int i = 0; i < n; i++) {
            cin >> t[i].name;
            for (int j = 0; j < n - 1; j++) {
                int r;
                cin >> r;
                if (r == 0) t[i].win++;
                else if (r == 1) t[i].lose++;
            }
        }
        mysort(t, n);
        for (int i = 0; i < n; i++) {
            cout << t[i].name << endl;
        }
    }
}

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] >> 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<stdio.h>
int main(void)
{
	int n,k[10],m[10],r,i,j,temp,zyu[10];
	char t[11],te;
	while(1){
		for(i=0;i<10;i++){
			k[i]=0;
			m[i]=0;
		}
		scanf("%d",&n);
		if(n<2)break;
		for(i=0;i<n;i++){
			zyu[i]=i;
			scanf(" %c",&t[i]);
//			printf("%c\n",t[i]);
			for(j=0;j<n-1;j++){
				scanf("%d",&r);
				if(r==0){
					k[i]++;
				}
				if(r==1){
					m[i]++;
				}
//				printf("%d %d\n",r,k[i]);
			}
		}
		for(i=0;i<n-1;i++){
			for(j=i+1;j<n;j++){
				if(k[i]<k[j]||(k[i]==k[j]&&m[i]>m[j] )|| (k[i]==k[j] && m[i]==m[j] && zyu[i]>zyu[j])){
					temp=k[i];
					k[i]=k[j];
					k[j]=temp;
					
					temp=m[i];
					m[i]=m[j];
					m[j]=temp;
					
					temp=zyu[i];
					zyu[i]=zyu[j];
					zyu[j]=temp;				

					te=t[i];
					t[i]=t[j];
					t[j]=te;
				}
			}
		}
		for(i=0;i<n;i++){
			printf("%c\n",t[i]);
		}
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int main()
{
	
	int n;
	char t;
	int r;
	char x[10];

	while(true){
		cin >> n;
		if(n==0)
			break;
		int win[10],lose[10];
		for(int i=0 ;i<10 ;i++){
				win[i] = 0;
				lose[i] = 0;
		}
		for(int i=0 ;i<n ;i++){
			cin >> t;

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

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

		for(int i=0 ;i<n ;i++){
			cout << x[i] << endl;
		}
	}
	return 0;
}