Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Name string
	Win, Lose, Draw, Index 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
	}
	index := func(c1, c2 *Team) bool {
		return c1.Index < c2.Index
	}

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

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

using namespace std;

bool cmp(pair<char, int[3]> p, pair<char, int[3]> q) 
{
    return (p.second[0] > q.second[0]) || (p.second[0] == q.second[0] && p.second[1] < q.second[1]);
}


int main()
{
    int n;

    while (cin >> n) {
        if (!n)
            break;
        
        pair<char, int[3]> team[10];

        for (int i = 0; i < n; i++) {
            char ch;
            
            cin >> team[i].first;
            for (int j = 0; j < n - 1; j++) {
                int ind;
                cin >> ind;
                team[i].second[ind]++;
            }
        }
        
        stable_sort(team, team + n, cmp);

        for (int i = 0; i < n; i++)
            cout << team[i].first << endl;
    }

    return 0;
}

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

int main(){
  int n,ans[10],w[10],l[10];
  string t[10];
  bool use[10];

  for(;;){
    cin >> n;
    if(!n)break;

    for(int i=0;i<n;i++){
      cin >> t[i];
      w[i] = l[i] = 0;
      use[i] = false;

      int tmp;
      for(int j=1;j<n;j++){
	cin >>tmp;
	if(tmp==0)w[i]++;
	if(tmp==1)l[i]++;
      }
    }

    for(int i=0;i<n;i++){
      int num = -1;
      for(int j=0;j<n;j++){
	if(!use[j]){
	  if(num<0)num = j;
	  else{
	    if(w[j]>w[num])num = j;
	    else if(w[j]==w[num] && l[j]<l[num])num = j;
	  }
	}
      }
      ans[i] = num;
      use[num] = true;
    }

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

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

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int n;
    while(cin>>n,n){
        vector<pii> ujimatsu(n,pii(0,0));
        vc anko(n);
        rep(i,n){
            cin>>anko[i];
            rep(j,n-1){
                int tiya; cin>>tiya;
                if(tiya==0)ujimatsu[i].first+=100000000;
                else if(tiya==1)ujimatsu[i].second+=1;
                else if(tiya==2)ujimatsu[i].first+=1;
            }
        }
        vector<bool>flag(1000,true);
        rep(i,n){
            int maxi=-INF;
            int ans=0;
            rep(j,n){
                if(!flag[j])continue;
                if(ujimatsu[j].first>maxi){
                    maxi=ujimatsu[j].first;
                    ans=j;
                }else if(ujimatsu[j].first==maxi){
                    if(ujimatsu[ans].second>ujimatsu[j].second){
                        ans=j;
                    }
                }
            }
            flag[ans]=false;
            cout<<anko[ans]<<endl;
        }
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF (1<<21)

bool cmp(const pair<int,char> &a,const pair<int,char> &b){
	return a.first > b.first;
}
int main(){
	int n;
	while(cin >> n,n){
		vector< pair<int,char> > data(n);
		rep(i,n){
			int sum = 0;
			cin >> data[i].second;
			rep(j,n-1){
				int t;
				cin >> t;
				sum += (t==0?10:t==2?1:0);
			}
			data[i].first = sum;
		}
		stable_sort(data.begin(),data.end(),cmp);
		rep(i,n){
			cout << data[i].second << endl;	
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef pair<int,int> P;
typedef pair<P, int> Pc;

int main() {
    int N;
    while (cin >> N, N) {
        vector<Pc> pc;
        for (int i = 0; i < N; i++) {
            int a = 0, b = 0, c;
            char s;
            cin >> s;
            c = (N-1) * 1000 + s;
            for (int j = 0; j < N - 1; j++) {
                int t; cin >> t;
                if (t == 0) a++;
                if (t == 2) b++;
            }

            pc.push_back(Pc(P(a, b), c));
        }

        sort(pc.begin(), pc.end());

        for (int i = 0; i < N; i++)
            cout << (char)(pc[N-1-i].second  % 1000)<< endl;
    }

    return 0;
}

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

class Team{
public:
	int x,y,z;
	Team(){ x=y=z=0;}
	Team(int win, int loose, int ID){ x=win, y=loose, z=ID;}
	bool operator>(Team tmp){
		if( this->x > tmp.x ) return true; else if( this->x < tmp.x ) return false;
		// winが等しい
		if( this->y < tmp.y ) return true; else if( this->y > tmp.y ) return false;
		//勝ちも負けも等しい
		return this->z < tmp.z;
	}
};
bool cmp(Team a, Team b){
	return a > b;
}
int main() {
	while(1){
		int n; cin >> n ; if(!n) break;
		char ch[10]; Team a[10];
		for(int j=0;j<n;j++){
			cin >> ch[j] ;
			int win=0,loose=0;
			for(int i=0;i<n-1;i++){
				int tmp; cin >> tmp ;
				if(tmp==0) win++;
				else if(tmp==1) loose++;
			}
			a[j].x=win,a[j].y=loose,a[j].z=j;
		}
		sort(a,a+n,cmp);
		for(int i=0;i<n;i++){
			cout << ch[a[i].z] << '\n' ;
		}
	}
}