Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"sort"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

type Team struct {
	Name string
	Win, Lose, Draw, Index 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
	}
	index := func(c1, c2 *Team) bool {
		return c1.Index < c2.Index
	}

	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, i}
		}
		OrderedBy(win, lose, index).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j;
  int n;
  while(1){
    cin>>n;
    if(n==0)break;
    vector<string> t(n);
    pair<pii,int> p[10];
    rep(i,n){
      int r;
      cin>>t[i];
      p[i].second=i;
      rep(j,n-1){
        cin>>r;
        if(r==0)
          p[i].first.first--;
        else if(r==1)
          p[i].first.second++;
      }
    }
    sort(p,p+n);
    rep(i,n)
      cout<<t[p[i].second]<<endl;
  }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, a, d[10], e[10]; char c[10];
int main() {
	while (true) {
		scanf("%d", &n);
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			cin >> c[i];
			for (int j = 0; j < n; j++) {
				scanf("%d", &a);
				if (a == 0) d[i]++;
				if (a == 1) e[i]++;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = i + 1; j < n; j++) {
				if (d[i] < d[j]) {
					swap(c[i], c[j]);
					swap(d[i], d[j]);
					swap(e[i], e[j]);
				}
				else if (d[i] == d[j] && e[i] > e[j]) {
					swap(c[i], c[j]);
					swap(d[i], d[j]);
					swap(e[i], e[j]);
				}
			}
		}
		for (int i = 0; i < n; i++) printf("%c\n", c[i]);
	}
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
#define rep2(x,from,to) for(int x=(from);x<(to);++(x))
#define rep(x,to) rep2(x,0,to)
bool desc(pair<int,pair<int,string> > p1, pair<int,pair<int,string> > p2) {
	if(p1.first == p2.first) return p1.second.first < p2.second.first;
	return p1.first > p2.first;
}
int main(){
	int n;
	while(cin >> n, n) {
		vector<pair<int,pair<int,string> > > v;
		int r;
		rep(i,n) {
			pair<int,pair<int,string> > p;
			cin >> p.second.second;
			rep(j,n-1) {
				cin >> r;
				if(r == 0) p.first++;
				if(r == 1) p.second.first++;
			}
			v.push_back(p);
		}
		sort(v.begin(), v.end(), desc);
		rep(i,n) {
			cout << v[i].second.second << endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void)
{
	long long int i,j,k,n,a,b,sum;
	while(1) {
		cin>>n;
		vector<pair<long long int,char> > pp(n);
		
		if(n==0) break;
		for(i=0;i<n;i++) {
			cin>>pp[i].second;
			sum=0;
			for(j=0;j<n-1;j++) {
				cin>>a;
				if(a==0) sum+=10000000;
				if(a==1) sum-=1;
			}
			pp[i].first=sum;
		}
		stable_sort(pp.begin(),pp.end());
		for(i=n-1;i>=0;i--) {
			cout<<pp[i].second<<endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
pair<int, char>a[100000];
int n, sum, p;
int main() {
	while (true) {
		memset(a, 0, sizeof(a));
		cin >> n;
		if (n == 0) { break; }
		for (int i = 0; i < n; i++) {
			cin >> a[i].second;
			sum = 0;
			for (int j = 0; j < n - 1; j++) {
				cin >> p;
				if (p == 0) { sum += 10000; }
				if (p == 2) { sum += 1; }
			}
		}
		sort(a, a + n);
		for (int i = n - 1; i >= 0; i++) {
			cout << a[i].second << endl;
		}
	}
}

C++: 
#include <bits/stdc++.h>
#include <stdio.h>
#include <numeric>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define repone(i,n) for(int i=1;i<(int)(n);++i)
#define repo(i,o,n) for(int i=o;i<(int)(n);++i)
#define Rep(j,n) for(int j=0;j<(int)(n);++j)
#define Repo(j,o,n) for(int j=o;j<(int)(n);++j)
#define Repone(j,n) for(int j=1;j<(int)(n);++j)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define int long long
#define pb(a) push_back(a)
#define pbpair(a,b) push_back(make_pair(a,b))
#define MOD 1000000007
#define INF 9223372036854775807
#define pairint pair<int,int>
typedef pair< pairint , string> P;
//実装案　勝たなかった試合＆負けた試合のベクター　照準ソートして上から
int gcd( int x, int y ){ return y ? gcd(y, x%y) : x; }
bool pairCompare(const P& firstElof, const P& secondElof)
{
  if(firstElof.first.first!= secondElof.first.first)    return firstElof.first.first< secondElof.first.first;
else if(firstElof.first.second != secondElof.first.second)    return firstElof.first.second< secondElof.first.second;
else return false;
}
signed main() {
int x=0,y=0,z=0,w=0,n=0,m=0;
string k;
while(1){
  std::cin >> x;
  if(x==0)return 0;
  std::vector< pair < pairint ,string> > v(x);
  Rep(j,x){
      std::cin >> k;
    v[j].second=k;
    v[j].first.first=0;
    v[j].first.second=0;
    rep(i,x){
    if(i==0){

    }else{
      std::cin >> z;
      if(z==1){
        v[j].first.first+=1;
        v[j].first.second+=1;
      }
      if(z==2)v[j].first.first+=1;
    }
    }
  }
  sort(all(v),pairCompare);
  rep(i,x){
    std::cout << v[i].second << '\n';
  }
}
}

C++: #include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
	int n, *a, ma, mi, s;
	char *t;
	
	while(1){
		cin >> n;
		if(n == 0){
			break;
		}
		a = (int *)calloc(n, sizeof(int));
		t = (char *)malloc(n*sizeof(char));
		ma = 0;
		mi = 60;
		for(int i = 0; i < n; i++){
			cin >> t[i];
			for(int j = 1; j < n; j++){
				cin >> s;
				if(s == 0){
					a[i] += 3;
				} else if(a[i] == 2){
					a[i] += 1;
				}
			}
			if(a[i] > ma){
				ma = a[i];
			}
			if(a[i] < mi){
				mi = a[i];
			}
		}
		
		for(int i = ma; i >= mi; i--){
			for(int j = 0; j < n; j++){
				if(a[j] == i){
					cout << t[j] << endl;
				}
			}
		}
		
		free(a);
		free(t);
	}
	
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
	vector<pair<int,char> >ans;
	int n;
	while(cin>>n,n){
	vector<pair<int,char> >ans;
	for(int i=0;i<n;i++){
		char input;
		cin>>input;
		int rec=0;
		for(int j=0;j<n-1;j++){
			int k;
			cin>>k;
			if(k==0)rec+=10000;
			else if(k==1)rec-=100;
		}
		ans.push_back(make_pair(rec,input));
	}
	sort(ans.begin(),ans.end());
	for(int i=0;i<n;i++){
		cout<<ans[ans.size()-i-1].second<<endl;
	}
}
}