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<utility>
#include<algorithm>
#include<queue>

using namespace std;

priority_queue<pair<pair<double, double>,char>> results;

int main() {
	int n;
	while (true) {
		cin >> n; if (n == 0)break;
		char name;
		int win, lose;
		for (int i = 0; i < n;++i) {
			cin >> name;
			win = 0;lose = 0;
			for (int j = 0; j < n - 1; ++j) {
				int r;
				cin >> r;
				if (r == 2)continue;
				else if (r == 0)win++;
				else lose++;
			}
			results.emplace(make_pair(make_pair(win,-lose),name));
		}
		while(!results.empty()) {
			cout << results.top().second << endl;
			results.pop();
		}
	}
	return 0;
}

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

struct PROFILE {
    char name[5];
    int win_num;
    int lose_num;
};

bool operator<(const PROFILE& left, const PROFILE& right)
{
    return left.lose_num < right.lose_num;
}

int main(void)
{
    struct PROFILE team[10];
    int n;
    
    while (1) {
        scanf("%d", &n);
        if (n == 0) break;
        
        for (int i = 0; i < 10; i++){
           team[i].win_num = team[i].lose_num = 0;
        }
        
        int tmp;
        for (int i = 0; i < n; i++){
            scanf("%s", team[i].name);
            for (int j = 0; j < (n - 1); j++){
                scanf("%d", &tmp);
                if (tmp == 0) team[i].win_num++;
                else if (tmp == 1) team[i].lose_num++;
            }
        }
        
        sort(team, team + n);
        
        for (int i = 0; i < n; i++){
            /*if (team[i].win_num == team[i + 1].win_num){
                if (team[i].lose_num < team[i + 1].lose_num){
                    printf("%c\n", team[i].name[0]);
                    i++;
                    printf("%c\n", team[i].name[0]);
                }
            }
            else */printf("%c\n", team[i].name[0]);
        }
    }
    
    return (0);
}

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

int main(){
  int n;

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

    char t[n];
    int id[n], win[n], lose[n];

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

      win[i] = 0;
      lose[i] = 0;

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

        if(x == 0) win[i]++;
        else if(x == 1) lose[i]++;
      }
    }

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

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

C++: #include<iostream>
#include<algorithm>
#include<vector>
class team {
public:
	char name;
	int win;
	int lose;
	team() :win(0), lose(0) {}
};

bool compare(team &a, team &b) {
	return a.win > b.win || (a.win == b.win&&a.lose < b.lose);
}
using namespace std;

int main() {
	int num;
	while (cin >> num&&num != 0) {
		vector<team> line(num);
		for (int i = 0; i < num; ++i) {
			cin >> line[i].name;
			for (int j = 0, nmax = num - 1; j < nmax; ++j) {
				int result;
				cin >> result;
				if (result == 0) {
					++line[i].win;
				}
				else if (result == 1) {
					++line[i].lose;
				}
			}
		}
		sort(line.begin(), line.end(), compare);
		for (int i = 0; i < num; ++i) {
			cout << line[i].name << endl;
		}
	}
	return 0;
}

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

class Team{
public:
  int id;
  char name;
  int win, lose;

  Team(){}
  Team(int id, char name, int win, int lose):
    id(id), name(name), win(win), lose(lose){}

  bool operator<(const Team &team)const{
    if(win == team.win){
      if(lose == team.lose){
        return id < team.id;
      }
      return lose < team.lose;
    }
    return win > team.win;
  }
};

int main(){
  int n;

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

    Team t[n];

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

      t[i].win = 0;
      t[i].lose = 0;

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

        if(x == 0) t[i].win++;
        else if(x == 1) t[i].lose++;
      }
    }

    sort(t, t + n);

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

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
int main(){
  int n = 1;
  while(n != 0){
    cin >> n;
    if(n == 0)break;
    vector<pair<int,pair<int,pair<int,char>>>>v(n);
    rep(i,n){
      char c;
      cin >> c;
      int cnt1 = 0,cnt2 = 0;
      rep(j,n - 1){
        int tmp;
        cin >> tmp;
        if(tmp == 0)cnt1--;
        if(tmp == 1)cnt2++;
      }
      v[i] = {cnt1,{cnt2,{i,c}}};
    }
    sort(v.begin(),v.end());
    rep(i,n)cout << v[i].second.second.second << endl;
  }
  return 0;
}