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 <algorithm>
using namespace std;
struct team{
	char name;
	int win,lose,draw;
	team(){
		win = lose = draw = 0;
	}
};
bool checker(team a,team b){
	if(a.win > b.win) return true;
	if(a.win == b.win && a.lose < b.lose) return true;
	return false;
}
int main(){
	int n,a;
	char b;
	while(cin >> n,n){
		team c[10];
		for(int i = 0;i < n;++i){
			cin >> b;
			c[i].name = b;
			for(int j = 0;j < n - 1;++j){
				cin >> a;
				if(a == 0) ++c[i].win;
				else if(a == 1) ++c[i].lose;
				else ++c[i].draw;
			}
		}
		sort(c,c+n,checker);
		for(int i = 0;i < n;++i){
			cout << c[i].name << endl;
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;

struct st {
	char a;
	int b, c;
};
int main() {
	int n;
	while (cin >> n, n) {
		vector<st>v;
		rep(i, n) {
			char t; int a = 0, b = 0;
			cin >> t;
			rep(j, n - 1) {
				int r; cin >> r;
				if (!r)a++;
				if (r == 1)b++;
			}
			v.push_back({ t,a,b });
		}
		stable_sort(v.begin(), v.end(), [](st a, st b) {return a.c < b.c; });
		stable_sort(v.begin(), v.end(), [](st a, st b) {return a.b > b.b; });
		for (st i : v) {
			cout << i.a << endl;
		}
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<char,pair<int,int> > p;
int main() {
	int n;
	while(cin>>n,n){
		vector<p>a;
		for(int i=0;i<n;i++){
			p data;cin>>data.first;
			data.second.first=0;
			data.second.second=0;
			for(int j=0;j<n-1;j++){
				int p;cin>>p;
				if(p==0)data.second.first++;
				else if(p==1)data.second.second++;
			}
			a.push_back(data);
		}
		for(int i=0;i<n;i++){
			int f=0;
			for(int j=1;j<a.size();j++){
				if(a[j].second.first>a[f].second.first)f=j;
				else if(a[j].second.first==a[f].second.first&&a[j].second.first<a[f].second.first)f=j;
			}
			cout<<a[f].first<<endl;
			v.erase(v.begin()+f);
		}
	}
	return 0;
}

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

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

int main() {
    int N;
    while (cin >> N, N) {
        vector<Pc> pc;
        for (int i = 0; i < N; i++) {
            char c;
            cin >> c;
            int a = 0, b = 0;
            for (int j = 0; j < N - 1; j++) {
                int t; cin >> t;
                if (t == 0) a++;
                if (t == 2) b++;
            }

            pc.push_back(Pc(P(a, b), c));
        }

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

        for (int i = 0; i < N; i++)
            cout << pc[N-1-i].second << endl;
    }

    return 0;
}

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

int main()
{
  int n;
  while (cin >> n, n){
    pair<pair<int, int>, char> ppiic[11];
    for (int i = 0; i < n; i++){
      char c;
      ppiic[i].first = make_pair(0, 0);
      cin >> c;
      ppiic[i].second = c;
      for (int j = 0; j < n - 1; j++){
        int num;
        cin >> num;
        if (num == 0) ppiic[i].first.first++;
        else if (num == 1) ppiic[i].first.second--;;
      }
    }
    sort(ppiic, ppiic + n);
    reverse(ppiic, ppiic + n);

    for (int i  = 0; i < n; i++){
      cout << ppiic[i].second << '\n';
    }
  }
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(void){

	int n;
	char t[10];
	int r;
	int win[10] = { 0 }, lose[10] = { 0 };

	while (cin >> n && n != 0){

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

		for (int i = 0; i < n; i++){
			for (int j = i + 1; j < n; j++){
				if (win[i] < win[j]){
					swap(win[i], win[j]);
					swap(lose[i], lose[j]);
					swap(t[i], t[j]);
				}
				else if (win[i] == win[j]){
					if (lose[i] > lose[j]){
						swap(win[i], win[j]);
						swap(lose[i], lose[j]);
						swap(t[i], t[j]);
					}
				}
			}

			cout << t[i] << endl;
		}
	}
}

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

typedef struct {
	char t;
	int pt[10];
	int win;
	int lose;
} GAME;

int main()
{
	GAME game[10], tmp;
	int n;

	while (scanf("%d", &n), n){
		for (int i = 0; i < n; i++){
			game[i].win = game[i].lose = 0;
			cin >> game[i].t;
			for (int j = 0; j < n - 1; j++){
				cin >> game[i].pt[j];
				if (game[i].pt[j] == 0){
					game[i].win++;
				}
				else if (game[i].pt[j] == 1){
					game[i].lose++;
				}
			}
		}

		for (int i = 0; i < n; i++){
			for (int j = n - 1; j > i; j--){
				if (game[j].win > game[j - 1].win){
					swap(game[j], game[j - 1]);
				}
				else if (game[j].win == game[j - 1].win && game[j].lose < game[j - 1].lose){
					swap(game[j], game[j - 1]);
				}
			}
		}

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

	return (0);
}

C++: //16
#include<iostream>
#include<algorithm>
#include<utility>

using namespace std;

typedef pair<char,int[3]> pci;

bool cmp(pci a,pci b){
  return a.second[0]>b.second[0]||(a.second[0]==b.second[0]&&a.second[1]<b.second[1]);
}				   

int main(){
  for(int n;cin>>n,n;){
    pci t[10];
    for(int i=0;i<n;i++){
      cin>>t[i].first;
      for(int j=0;j<n-1;j++){
	int r;
	cin>>r;
	t[i].second[r]++;
      }
    }
    stable_sort(t,t+n,cmp);
    for(int i=0;i<n;i++){
      cout<<t[i].first<<endl;
    }
  }
  return 0;
}