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 <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
	int n, **a, ma, mi, s;
	char *t, *t2;
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		a = (int **)calloc(n, sizeof(int));
		for(int i = 0; i < n; i++){
			a[i] = (int *)calloc(3, sizeof(int));
		}
		t = (char *)malloc(n*sizeof(char));
		ma = 0;
		mi = 20;
		for(int i = 0; i < n; i++){
			cin >> t[i];
			for(int j = 1; j < n; j++){
				cin >> s;
				a[i][s]++;
			}
			if(a[i][0] > ma){
				ma = a[i][0];
			}
			if(a[i][0] < mi){
				mi = a[i][0];
			}
		}
		
		for(int i = ma, l=0, d=0; i >= mi; i--, l=0, d=0){
			t2 = (char *)malloc(n*sizeof(char));
			for(int j = 0; j < n; j++){
				if(a[j][0] == i){
					t2[l++] = t[j];
					if(d < a[j][1]){
						d = a[j][1];
					}
				}
			}
			if(l == 1){
				cout << t2[0] << endl;
			} else if(1 < l){
				for(int j = 0; j <= d; j++){
					for(int k = 0; k < l; k++){
						for(int o = 0; o < n; o++){
							if(a[o][1] == j && t[o] == t2[k]){
								cout << t[o] << endl;
							}
						}
					}
				}
			}
			
			free(t2);
		}
		for(int i = 0; i < n; i++){
			free(a[i]);
		}
		free(a);
		free(t);
	}
	
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
const double EPS = 1e-10;
class Team {
public:
	int order, win, lose;
	char name;
	bool operator<(const Team &other) {
		if (win != other.win)return win > other.win;
		else if (lose != other.lose)return lose < other.lose;
		else return order < other.order;
	}
};
signed main() {
	int n;
	while (cin >> n, n) {
		vector<Team>v(n);
		rep(i, 0, n) {
			char c; cin >> c;
			v[i].name = c; v[i].order = i;
			rep(j, 0, n - 1) {
				int x; cin >> x;
				if (x == 0)v[i].win++;
				else if (x == 1)v[i].lose++;
			}
		}
		sort(all(v));
		rep(i, 0, n)cout << v[i].name << endl;
	}
	return 0;
}

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

const int N_MAX = 10;

struct Team{
	int id;
	int win;
	int lose;
	char t;
};

class TeamSort
{
public:
	bool operator()( const Team& t0, const Team& t1 )
	{
		if( t0.win > t1.win ) return true;
		if( t0.win < t1.win ) return false;
		if( t0.lose < t1.lose ) return true;
		if( t0.lose > t1.lose ) return false;
		return t0.id < t1.id;
	}
};

int main()
{
	int n;
	while( cin >> n && n ){
		vector< Team > team;
		for( int i = 0; i < n; ++i ){
			Team tm;
			memset( &tm, 0, sizeof(tm) );
			tm.id = i;
			cin >> tm.t;
			for( int j = 0; j < n - 1; ++j ){
				int r;
				cin >> r;
				switch( r ){
					case 0: ++tm.win; break;
					case 1: ++tm.lose; break;
				}
			}
			team.push_back( tm );
		}
		sort( team.begin(), team.end(), TeamSort() );
		for( int i = 0; i < n; ++i ){
			cout << team[ i ].t << endl;
		}
	}

	return 0;
}

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

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;
  }
}

C++: #define _USE_MATH_DEFINES
#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<climits>
using namespace std;

struct Team{
	char name;
	int win, lose;
	bool operator < (const Team &a) const{
		return (win == a.win) ? lose < a.lose : win > a.win;
	}
};

int main(){
	int N, R;
	while(cin >> N, N){
		Team t[10] = {};
		for(int i = 0; i < N; i++){
			cin >> t[i].name;
			for(int j = 0; j < N-1; j++){
				cin >> R;
				if(R == 0) t[i].win++;
				if(R == 1) t[i].lose++;
			}
		}

		sort(t, t + N);

		for(int i = 0; i < N; i++){
			printf("%c\n", t[i].name);
		}
	}
	return 0;
}

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

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

int main() {
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        Team* team = new Team [n];
        for (int i = 0; i < n; i++) {
            cin >> team[i].name;
            for (int j = 0; j < n - 1; j++) {
                int score;
                cin >> score;
                if (score == 0) {
                    team[i].win++;
                } else if (score == 1) {
                    team[i].lose++;
                }
            }
        }
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if (team[j].win < team[j + 1].win
                    || (team[j].win == team[j + 1].win && team[j].lose > team[j + 1].lose)) {
                    Team temp = team[j];
                    team[j] = team[j + 1];
                    team[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            cout << team[i].name << endl;
        }
    }
}