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

int main() {
    int n;
    while (cin >> n, n) {
        vector<char> name(n);
        vector<int> cnt_w(n), cnt_l(n);
        for (int i = 0; i < n; i++) {
            cin >> name[i];
            for (int j = 0; j < n - 1; j++) {
                int r; cin >> r;
                if (r == 0) cnt_w[i]++;
                if (r == 1) cnt_l[i]++;
            }
        }
        vector<vector<int>> res(n);
        for (int i = 0; i < n; i++) res[cnt_w[i]].emplace_back(i);
        for (int w = 0; w < n; w++) {
            sort(res[w].begin(), res[w].end(), [&](int i, int j) {
                if (cnt_l[i] == cnt_l[j]) return i < j;
                else return cnt_l[i] < cnt_l[j];
            });
        }
        for (int w = n - 1; w >= 0; w--){
            for (int idx: res[w]) cout << (char)name[idx] << endl;
        }
    }
    return 0;
}

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

int main(){
	
	int n , m ;
	
	while( cin >> n , n ){
		int count[60][2] = {} ;
		char t[60] = {} ;
		
		for( int i=0 ; i<n ; i++ ){
			cin >> t[i] ;
			for( int j=0 ; j<n-1 ; j++ ){
				cin >> m ;
				if( m!=2 ) count[i][m]++;
			}
		}
		
		for( int i=0 ; i<n ; i++ ){
			for( int j=i+1 ; j<n ; j++ ){
				if( count[i][0] <= count[j][0] ){
					if( count[i][0] != count[j][0] ){
						swap( count[i] , count[j] ) ;
						swap( t[i] , t[j] ) ;
					}else {
						if( count[i][1] > count[j][1] ){
							swap( count[i] , count[j] ) ;
							swap( t[i] , t[j] ) ;
						}
					}
				}
			}
		}
		
		for( int i=0 ; i<n ; i++ ) cout << t[i] << endl ;
	}
	
return 0 ;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	while(cin >> n && n)
	{
		string list;
		int number[1000]={};
		int win[1000]={};
		int lose[1000]={};
		int a;

		for(int i=0;i<n;i++)
		{
			cin >> list[i];
			number[i]=i;

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

		for(int i=0;i<n;i++)
		{
			for(int j=i;j<n;j++)
			{
				if(win[i]<win[j])
				{
					swap(win[i],win[j]);
					swap(lose[i],lose[j]);
					swap(list[i],list[j]);
					swap(number[i],number[j]);
				}
				else if(win[i]==win[j] && lose[i]>lose[j])
				{
					swap(win[i],win[j]);
					swap(lose[i],lose[j]);
					swap(list[i],list[j]);
					swap(number[i],number[j]);					
				}
				else if(win[i]==win[j] && lose[i]==lose[j] && number[i]>number[j])
				{
					swap(win[i],win[j]);
					swap(lose[i],lose[j]);
					swap(list[i],list[j]);
					swap(number[i],number[j]);	
				}
			}
		}
		for(int i=0;i<n;i++)cout << list[i] << endl;
	}

    return 0;
}

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

enum {WIN, LOSE, DRAW};

int main() {
	for(int n; cin >> n && n;) {
		vector<pair<int, int>> ranking; // score, ID
		vector<char> names(n);

		for(int i = 0; i < n; ++i) {
			cin >> names[i];

			int score = 0;
			for(int j = 0; j < n - 1; ++j) {
				int result;
				cin >> result;

				switch(result) {
				case WIN:
					score += 100;
					break;
				case LOSE:
					break;
				case DRAW:
					score += 1;
					break;
				default:
					assert(false);
				}
			}

			ranking.emplace_back(-score, i);
		}

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

		for(const auto &team : ranking) {
			cout << names[team.second] << endl;
		}
	}

	return EXIT_SUCCESS;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
struct team_b{
    char c;
    int w, l, d;
    team_b(){ c = w = l = d = 0; }
};
bool operator<( const team_b &l, const team_b &r ){
    if( l.w == r.w )
        return l.l < r.l;
    else
        return l.w > r.w;
}
 
int main(){
    int n;
    while( cin >> n, n ){
        team_b t[10];
        for(int i=0; i<n; i++){
            cin >> t[i].c;
            for(int j=0; j+1<n; j++ ){
                int k;
                cin >> k;
                if(k==0)      t[i].w++;
                else if(k==1) t[i].l++;
                else          t[i].d++;
            }
        }
        stable_sort( t, t+n );
        for(int i=0; i<n; i++){
            cout << t[i].c << endl;
        }
    }
    return 0;
}

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

struct yku {
  int k, m;
  char t;

  bool operator<(const yku& right) const {
    return k == right.k ? m < right.m : k > right.k;
  }
};

int main() {
  int n;
  while(cin >> n) {
    if(!n) return 0;
    yku a[10] = {};
    rep(i, 0, n) {
      cin >> a[i].t;
      rep(j, 0, n - 1) {
        int b;
        cin >> b;
        if(!b) a[i].k++;
        else if(b == 1) a[i].m++;
      }
    }

    sort(a, a + n);
    rep(i, 0, n) cout << a[i].t << endl;
  }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
#define mp make_pair

template<class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

vector<pair<int,string> > team(10);

int main() {
	int n;
	int score;
	while(n=in()){
		int i;
		for(i=0;i<n;i++){
			team[i].first=0;
			cin>>team[i].second;
//cout<<team[i].second<<endl;
			int j;
			for(j=0;j<n-1;j++){
				score=in();
				if(score==0)team[i].first+=100;
				if(score==2)team[i].first++;
//cout<<" "<<score<<endl;
			}
//cout<<team[i].second<<" "<<team[i].first<<endl;
		}
		int a,b;
		for(a=0;a<n;a++){
			for(b=0;b<n-1;b++){
				if(team[b].first<team[b+1].first){
					pair<int,string> buf;
					buf=team[b];
					team[b]=team[b+1];
					team[b+1]=buf;
				}
			}
		}
		for(i=0;i<n;i++){
			cout<<team[i].second<<endl;
		}
	}
	return 0;
}