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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	int n;
	while(cin>>n,n){
		vector<pair<pii,int>> v(n,mkp(pii(0,0),0));
		vector<char> w(n);
		rep(i,n){
			cin>>w[i];
			string s;
			getline(cin,s);
			stringstream ss;
			ss<<s;
			int t;
			v[i].S=i;
			while(ss>>t){
				if(t==0){
					v[i].F.F--;
				}
				else if(t==2){
					v[i].F.S--;
				}
			}
		}
		sort(ALL(v));
		rep(i,n){
			cout<<w[v[i].S]<<endl;
		}
	}
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}

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

using namespace std;

class team{
public:
	char name;
	int w;
	int l;
	
	bool operator <(const team arg) const{
		if(w == arg.w){
			return l < arg.l;
		}
		return w > arg.w;
	}
};

int main(void){
	int n;
	while(cin >> n && n){
		team t;
		vector<team> v;
		char c;
		int tmp;
		
		for(int j=0;j<n;j++){
			cin >> c;
			t.name = c;
			t.w = 0;
			t.l = 0;
			
			for(int i=0;i<n-1;i++){
				cin >> tmp;
				if(tmp == 0) t.w++;
				if(tmp == 1) t.l++;
			}
			v.push_back(t);
		}
		sort(v.begin(),v.end());
		
		for(int i=0;i<n;i++) cout << v[i].name << endl;
	}
}

C++: #include<stdio.h>
int main(void)
{
    int n, i, t, r, j, temp;
    char str[11] = { 0 };
    char a[10] = { 0 };
    int b[10 + 1][10 + 1];
    int c[10 + 1];
    int d[10 + 1];
    int hideo[10 + 1];
    scanf("%d", &n);
    while (n > 2 && n<10) {
        i = t = r = j = temp = 0;
        for (i = 1; i <= n; i++) {
            c[i] = 0;
            d[i] = 0;
            hideo[i] = 0;
            for (j = 1; j <= n; j++) {
                b[i][j] = 0;
            }
        }

        for (i = 1; i <= n; i++) {
            scanf("%s", &a[i]);
            str[i] = a[i];
            for (j = 1; j <= n - 1; j++) {
                scanf("%d", &b[i][j]);
                if (b[i][j] == 0) {
                    c[i]++;

                }
                if (b[i][j] == 1) {
                    d[i]++;
                }
            }
        }
        for (i = 1; i <= n; i++) {
            hideo[i] = c[i];
        }
        for (i = 1; i < n; i++) {
            for (j = n; j > i; j--) {
                if (c[j] > c[j - 1]) {
                    temp = c[j];
                    c[j] = c[j - 1];
                    c[j - 1] = temp;
                    a[1] = str[j];
                    str[j] = str[j - 1];
                    str[j - 1] = a[1];



                }
            }
        }
        for (i = 1; i <= n; i++) {
            if (c[i] == c[i + 1]) {
                if (d[i] > d[i + 1]) {
                    temp = c[i];
                    c[i] = c[i + 1];
                    c[i + 1] = temp;
                }
            }
        }
        for (i = 1; i <= n; i++) {
            printf("%c\n", str[i]);
        }
        scanf("%d", &n);
    }
}

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

struct Team {

	char name;
	int  winCnt;
	int  loseCnt;

};

int main()
{

	int n, r;
	int inInfo[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	Team t[10];

	while( cin >> n, n ) {

		for( int i = 0; i < 10; i++ ) { t[i].winCnt = 0; t[i].loseCnt = 0; }

		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].winCnt++;
				else if( r == 1 ) t[i].loseCnt++;

			}
		}

		for( int i = 0; i < n - 1; i++ ) {
			for( int j = i + 1; j < n; j++ ) {

				int  itemp;
				Team stemp;

				if( t[i].winCnt == t[j].winCnt ) {
					if( t[i].loseCnt == t[j].loseCnt ) {
						if( inInfo[i] > inInfo[j] ) {

							stemp = t[i];  itemp     = inInfo[i];
							t[i]  = t[j];  inInfo[i] = inInfo[j];
							t[j]  = stemp; inInfo[j] = itemp;

						}
					}
					if( t[i].loseCnt > t[j].loseCnt ) {

						stemp = t[i];  itemp     = inInfo[i];
						t[i]  = t[j];  inInfo[i] = inInfo[j];
						t[j]  = stemp; inInfo[j] = itemp;

					}
				}
				else if( t[i].winCnt < t[j].winCnt ) {

						stemp = t[i];  itemp     = inInfo[i];
						t[i]  = t[j];  inInfo[i] = inInfo[j];
						t[j]  = stemp; inInfo[j] = itemp;

				}

			}
		}

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

	}

	return 0;

}