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

typedef struct{
    int win,lose;
}Score_t;


bool comp(const pair<Score_t,char>& x, const pair<Score_t,char>& y){
    if(x.first.win == y.first.win)return x.first.lose < y.first.lose;
    return x.first.win > y.first.win;
}


int main(){
        
    int n;
    vector<pair<Score_t,char> > score;
    
    while(cin>>n, n!=0){
        score.clear();
        
        
        for(int i=0; i<n; i++){
            char id; cin >> id;
            Score_t tmp={0,0};
            for(int j=0; j<n-1; j++){
                int in;
                cin >>in;
                if(in==0)tmp.win++;
                else if(in==1)tmp.lose++;
            }
        
            score.push_back(make_pair(tmp,id));
        }
        
        sort(score.begin(),score.end(),comp);
        for(int i=0; i<n; i++)cout << score[i].second << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef pair<int, char> P;
typedef pair<int, P> P2;

int main(){
  int n;
  P2 team[11];
  while(cin >> n, n){
    for(int i = 0 ; i < n ; i++){
      team[i].first = 0;
      team[i].second.first = 0;
    }
    
    int op;
    for(int i = 0 ; i < n ; i++){
      cin >>team[i].second.second;
      for(int j = 0 ; j < n-1 ; j++){
	cin >> op;
	if(op == 0) team[i].first++;
	if(op == 1) team[i].second.first++;
      }
    }
    
    for(int i = 0 ; i < n ; i++){
      team[i].second.first *= -1;
    }
    
    stable_sort(team, team+n);
    reverse(team, team+n);
    
    for(int i = 0 ; i < n ; i++){
      cout << team[i].second.second << endl;
    }
  }
  return 0;
}

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

#define rep_(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) rep_(v, 0, n)
#define lep(v, n) rep_(v, 1, n + 1)
#define rrep_(v, f, l) for (int v = (l), v##_ = (f); v >= v##_; --v)
#define rrep(v, n) rrep_(v, 0, n - 1)
#define rlep(v, n) rrep_(v, 1, n)
#define show_a(a, size) rep(a##_it, size) std::cout << a[a##_it] << " \n"[a##_it == a##_it_ - 1];
#define show_v(v) show_a(v, v.size())
#define eachv(v, i) rep(i, v.size())
#define reachv(v, i) rrep(i, v.size())
#define debug(x) std::cerr << #x << " = " << x << "\n";

using namespace std;

typedef long long int lint;

static inline int in(){ int x; scanf("%d", &x); return x; }
static inline lint inl(){ lint x; scanf("%lld", &x); return x; }

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

int main()
{
    int n;
    while (n = in()){
        vector<Score> scores;
        rep(i, n){
            char id[8];
            int bucket[3] = {};
            scanf("%s", id);
            rep(j, n - 1) bucket[in()]++;
            scores.push_back(Score(id[0], bucket[0], bucket[1]));
        }
        sort(scores.begin(), scores.end());
        eachv(scores, i) printf("%c\n", scores[i].id);
    }
    return (0);
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
int main(void)
{
	char name[10];
	long long int toku[9][5],n,make,kati,hiki,sum[9],touroku[9];
	int flg[9];
	while(1) {
		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>>toku[i][j];
			}
		}
		for(int i=0;i<n;i++) {sum[i]=0;touroku[i]=0;}
		for(int i=0;i<n;i++) {
			kati=0;make=0;
			for(int j=0;j<n-1;j++) {
				if(toku[i][j]==0) kati++;
				if(toku[i][j]==1) make++;
			}	
			if(kati>0)
				sum[i]=kati*10000000+(make*-1);
			else 
				sum[i]=kati*10000000+(make*-1);
		/*	else if(kati==0 && make>0) 
				sum[i]=kati*10000000*-1+(make*-1);*/
		//	cout<<sum[i]<<" "<<name[i]<<endl;
		}
		for(int i=0;i<n;i++) touroku[i]=sum[i];
		sort(sum,sum+n,greater<int>());
	//	for(int i=0;i<n;i++) cout<<sum[i]<<endl;;
		for(int i=0;i<n;i++) flg[i]=0;
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				if(sum[i]==touroku[j] && flg[j]==0) {
					cout<<name[j]<<endl;
					flg[j]=1;
					break;
				}
			}
		}
	}
}

C++: // 2011/02/06 Tazoe

#include <iostream>
using namespace std;

struct team{
	char t;
	int win;
	int los;
	int drw;
	int ord;
};

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		struct team tea[10];
		for(int i=0; i<n; i++){
			tea[i].win = 0;
			tea[i].los = 0;
			tea[i].drw = 0;
			tea[i].ord = i;

			cin >> tea[i].t;
			for(int j=0; j<n-1; j++){
				int r;
				cin >> r;
				if(r==0)
					tea[i].win++;
				else if(r==1)
					tea[i].los++;
				else if(r==2)
					tea[i].drw++;
			}
		}

		for(int i=0; i<n-1; i++)
			for(int j=i+1; j<n; j++)
				if(tea[i].win<tea[j].win||(tea[i].win==tea[j].win&&tea[i].los>tea[j].los)
				 ||(tea[i].win==tea[j].win&&tea[i].los==tea[j].los&&tea[i].ord>tea[j].ord)){
					struct team tmp = tea[i];
					tea[i] = tea[j];
					tea[j] = tmp;
				}

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

	return 0;
}

C++: #include<iostream>
#include<map>
#include<string>
#include<stdio.h>
#include<cstdio>
#include<math.h>
using namespace std;
int main(){
    int n,score,sum[10000],memo=0,max=-10000;
    char ch[10000],tem;
    while(1){
    cin>>n;
        if(n==0){
            break;
        }
    for(int i=0;i<n;i++){
        cin>>ch[i];
        for(int j=0;j<n-1;j++){
            cin>>score;
            if(score==0){
                memo+=1;
            }
            else if(score==1){
                memo-=1;
            }
        }
        sum[i]=memo;
        memo=0;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(sum[j]>max){
                max=sum[j];
                tem=ch[j];
                memo=j;
            }
        }
        cout<<tem<<endl;
        sum[memo]=-10000;
        max=-10000;
    }
    }
    return 0;
}