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

#include<iostream>


using namespace std ;



#define name first
#define lose second



int main()
{
	int n ;

	while( 1 )
	{
		scanf( "%d" ,&n ) ;


		if( n == 0 )
		{
			return 0 ;
		}



		pair< int, int > p[ 10 ] ;



		int a ;

		char c[ 2 ] ;

		int i ,j ;
		for( i = 0 ; i < n ; ++i )
		{
			scanf( "%s" ,c ) ;


			p[ i ].name = c[ 0 ] * 100 ;


			for( j = 1 ; j < n ; ++j )
			{
				scanf( "%d" ,&a ) ;


				if( a < 2 )
				{
					if( a )
					{
						--p[ i ].lose ;
					}

					else
					{
						++p[ i ].name ;
					}
				}
			}
		}


		for( i = 0 ; i < n ; ++i )
		{
			for( j = i ; j < n ; ++j )
			{
				pair< int ,int > tmp ;

				if( p[ i ].name % 100 < p[ j ].name % 100 )
				{
					tmp = p[ i ] ;

					p[ i ] = p[ j ] ;

					p[ j ] = tmp ;
				}

				else if( p[ i ].lose < p[ j ].lose && p[ i ].name == p[ j ].name )
				{
					tmp = p[ i ] ;

					p[ i ] = p[ j ] ;

					p[ j ] = tmp ;
				}
			}
		}


		for( i = 0 ; i < n ; ++i )
		{
			printf( "%c\n" ,p[ i ].name / 100 ) ;
		}
	}
}

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

int main(){
	int n;
	while(cin >> n && n){
		vector<string> a;
		vector< pair<int,int> > v;
		for(int i = 0 ; i < n ; i++){
			string name;
			cin >> name;
			a.push_back(name);
			int w = 0;
			for(int j = 0 ; j < n - 1 ; j++){
				int t;
				cin >> t;
				if( t == 0 ) w += 10000;
				if( t == 2 ) w += 1;
			}
			v.push_back(make_pair(-w,i));
		}
		sort(v.begin(),v.end());
		for(int i = 0 ; i < v.size() ; i++){
			cout << a[v[i].second] << endl;
		}
	}
	
}

C++: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define Rrep(i, m, n) for(int i=(m);i<(n);i++)
#define All(v) v.begin(),v.end()
#define Uniq(v) v.erase(unique(All(v)),v.end())
typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
typedef pair<string, int> Psi;
typedef pair<Pip, char> Pic;

typedef vector<int> Vi;
const int INF = (1<<30);
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};


int main()
{
  int n;

  while( cin >> n, n ) {
    priority_queue<Pic> q;

    Rep(i, n) {
      char c;
      int win = 0, los = 0;
      cin >> c;
      
      Rep(j, n-1) {
	int a;
	cin >> a;
	if( a == 0 ) win++;
	else if( a == 1 ) los++;
      }

      q.push( Pic(Pip(win, Pii(-los, -i)), c) );
    }

    while( !q.empty() ) {
      cout << q.top().sc << endl;
      q.pop();
    }

  }

}

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

int main(){
  int n;

  while(cin >> n){
    if(n == 0){
      break;
    }

    pair<pair<int,int>,pair<int,char> > t[n];

    for(int i = 0; i < n; i++){
      t[i].second.first = i;
      cin >> t[i].second.second;

      t[i].first.first = 0;
      t[i].first.second = 0;

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

        if(x == 0) t[i].first.first--;
        else if(x == 1) t[i].first.second++;
      }
    }

    sort(t, t + n);

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

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

//define
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define ADD(a, b) a = (a + b) % mod
#define SUB(a, b) a = (a+mod-b)%mod
#define MUL(a, b) a = (a * b) % mod
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define dmp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define fi first
#define se second

//template
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p){
  os << p.first << " " << p.second;return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
  for (auto it = v.begin();it != v.end();++it){
    if (it != v.begin())os << " ";os << *it;
  }return os;
}
template<typename T, int SIZE>
int array_length(const T (&)[SIZE]){return SIZE;}
template<typename T, int N>
void print(const T (&a)[N], int s = N, int t = -1, bool f = true){
	if(t == -1){rep(i,s){if(i)cout << " ";cout << a[i];}}
	else repi(i,s,t){if(i!=s)cout << " ";cout << a[i];}
	if(f)cout << "\n";
}
template<typename T, int N1, int N2>
void print(const T (&a)[N1][N2], int h = N1, int w = N2){
	rep(i,h){rep(j,w){cout << a[i][j] << " \n"[j==w-1];}}
}

//typedef
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef pair<P, int> Pi;
typedef vector<int> vi;
typedef deque<int> dq;
const int inf = 1e9+7;
const int INF = 1e18+7;

struct st{int win, lose;char c;};
bool comp(const st& a, const st& b){
	if(a.win == b.win)return a.lose < b.lose;
	return a.win > b.win;
}

st a[30];
signed main(){
	while(1){
		int n;
		scanf("%lld", &n);
		if(n == 0)break;
		rep(i,n)a[i].win = a[i].lose = 0;
		char c;int tmp;
		rep(i,n){
			scanf(" %c", &c);
			a[i].c = c;
			rep(j,n-1){
				scanf("%lld", &tmp);
				if(tmp == 0)a[i].win++;
				if(tmp == 1)a[i].lose++;
			}
		}
		sort(a, a+n, comp);
		rep(i,n)printf("%c\n", a[i].c);
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main()
{
	int n,score;
	int tokuten[10];
	char name[10];
	while(1){
		for(int i=0;i<10;i++)
			tokuten[i] = 0;
		cin >> n;
		if(n == 0)
			break;
		for(int i=0;i<n;i++){
			cin >> name[i];
			for(int j=0;j<n-1;j++){
				cin >> score;
				if(score ==0)
					tokuten[i] += 2;
				else if(score == 1)
					tokuten[i]--;
			}
		}
		for(int i=0;i<n-1;i++){
			for(int j=i+1;j<n;j++){
				if(tokuten[i] < tokuten[j]){
					int T = tokuten[i];
					tokuten[i] = tokuten[j];
					tokuten[j] = T;
					char TT = name[i];
					name[i] = name[j];
					name[j] = TT;
				}
			}
		}
		for(int i=0;i<n;i++)
			cout << name[i] << endl;
	}
	return 0;
}