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<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;
		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<stdio.h>
int main(void){
	int n,r,t[3][10],i,a,f;
	char N[10][10];
	scanf("%d",&n);
	for(i=0;i!=n;i++){
		t[0][i]=i;
		t[1][i]=0;
		t[2][i]=0;
	}
	for(i=0;i!=n;i++){
		scanf("%s",&N[i]);
		for(a=0;a!=n-1;a++){
			scanf("%d",&r);
			if(r==0){
				t[1][i]++;
			}
			else if(r==1){
				t[2][i]++;
			}
		}
	}
	for(i=0;i!=n;i++){
		for(a=i;a!=n;a++){
			if(t[1][i]<t[1][a]){
				for(f=0;f!=3;f++){
					r=t[f][i];
					t[f][i]=t[f][a];
					t[f][a]=r;
				}
			}
			else if(t[1][i]==t[1][a]){
				if(t[2][i]>t[2][a]){
					for(f=0;f!=3;f++){
						r=t[f][i];
						t[f][i]=t[f][a];
						t[f][a]=r;
					}
				}
			}
		}
	}
	for(i=0;i!=n;i++){
		r=t[0][i];
		printf("%s\n",N[r]);
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
long long int i,j,k,n,sum[9],data,a,b;
int main(void)
{
	char name[10];
	while(1) {
		cin>>n;
		if(n==0) break;
		for(i=0;i<n;i++) sum[i]=0;
		
		for(i=0;i<n;i++) {
			cin>>name[i];
			a=b=0;
			for(j=0;j<n-1;j++) {
				cin>>data;
				if(data==0) a++;
				else if(data==1) b++;
			}
			sum[i]=a*100000000+b*-1;
		}
		for(i=0;i<n-1;i++) {
			for(j=i+1;j<n;j++) {
				if(sum[i]<sum[j]) {
					swap(sum[i],sum[j]);
					swap(name[i],name[j]);
				}
			}
		}
		
		for(i=0;i<n;i++) cout<<name[i]<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void)
{
	int i,j,k,n,sum[11];
	int data;
	char name[11][11];
	while(1) {
		cin>>n;
		if(!n) break;
		for(i=0;i<n;i++) sum[i]=0;
		for(i=0;i<n;i++) {
			cin>>name[i];
			for(j=0;j<n-1;j++) {
				cin>>data;
				if(data==0) sum[i]+=10000;
				else if(data==1) sum[i]-=10;
			}
		}
		for(i=0;i<n-1;i++) {
			for(j=i+1;j<n;j++) {
				if(sum[i]<sum[j]) {
					swap(sum[i],sum[j]);
					swap(name[i],name[j]);
				}
			}
		}
		for(i=0;i<n;i++) cout<<name[i]<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int,int> Pii;
typedef pair<int,string> Pis;
typedef pair<Pii,Pis> P;
int n,m;P t[10];
int main(){
  while(cin>>n&&n){
    for(int i=0;i<n;i++){
      cin>>t[i].second.second;
      t[i].second.first=i;
      t[i].first.first=t[i].first.second=0;
      for(int j=0;j<n-1;j++){
	cin>>m;
	if(m==0)t[i].first.first--;
	else if(m==1)t[i].first.second++;
      }
    }
    sort(t,t+n);for(int i=0;i<n;i++)cout<<t[i].second.second<<endl; 
  }
}

C++: #include<stdio.h>
int main(void)
{
    int n,i,j,h,w[10],l[10],t,u[10];
    char q[11],g;
    while(1){
        for(i=0;i<10;i++){
            w[i]=0; l[i]=0;
        }   
        scanf("%d",&n);
        if(n==0) break;
            for(i=0;i<n;i++){
				u[i]=i;
                scanf(" %c",&q[i]);
                for(j=0;j<n-1;j++){
                    scanf("%d",&h);
                    if(h==0) w[i]++;
                    if(h==1) l[i]++;
                }
            }
            for(i=0;i<n-1;i++){
                for(j=i+1;j<n;j++){
                    if(w[i]<w[j]||(w[i]==w[j]&&l[i]>l[j])||(w[i]==w[j]&&l[i]==l[j]&&u[i]>u[j])){
                        t=w[i];
                        w[i]=w[j];
                        w[j]=t;
                         
                        t=l[i];
                        l[i]=l[j];
                        l[j]=t;
                         
                        t=u[i];
                        u[i]=u[j];
                        u[j]=t;
                     
                        g=q[i];
                        q[i]=q[j];
                        q[j]=g;
                    }               
                }   
            }       
            for(i=0;i<n;i++) printf("%c\n",q[i]);
    }
    return 0;
}

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

struct team{std::string name; int win; int lose;};

bool ascendingteam(const team& lteam, const team& rteam){
  if(lteam.win == rteam.win)
    return lteam.lose < rteam.lose;
  return lteam.win > rteam.win;  
}

int main(){
  int n;
  while(std::cin >> n, n){
    std::vector<team> tv;
    for(int j=0;j<n;j++){
      team t;
      t.win = 0, t.lose = 0;
      std::string name;
      std::cin >> name;
      t.name = name;
      for(int i=0;i<n-1;i++){
        int point;
        std::cin >> point;
        if(point == 0)
          t.win++;
        else if(point == 1)
          t.lose++;
      }
      tv.push_back(t);
    }

    std::sort(tv.begin(), tv.end(), ascendingteam);
    
    std::vector<team>::iterator it = tv.begin();
    while(it != tv.end()){
      std::cout << (*it).name << std::endl;
      it++;
    }
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
	int n;
	while(cin>>n,n){
		vector<char> data[11][11];
		rep(i,n){
			int w=0,l=0;
			char t;
			cin>>t;
			rep(j,n-1){
				int a;
				cin>>a;
				if(a==0)w++;
				else if(a==1)l++;
			}
			data[w][l].push_back(t);
		}
		for(int i=n-1;i>=0;i--){
			rep(j,n)if(data[i][j].size()>0){
				for(int k=0;k<data[i][j].size();k++){
					cout<<data[i][j][k]<<endl;
				}
			}
		}
	}
}