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

}

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