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 t;
	int w, l;
};

bool large(team t1, team t2){
	return ((t1.w > t2.w) || (t1.w == t2.w && t1.l < t2.l));
}

int main(){
	while(1){
		int n;
		cin >> n;
		if(n == 0) break;
		team teams[10] = {0};
		for(int i = 0;i < n;i++){
			cin >> teams[i].t;
			for(int j = 0;j < n - 1;j++){
				int r;
				cin >> r;
				if(r == 0) teams[i].w++;
				if(r == 1) teams[i].l++;
			}
		}
		stable_sort(teams, teams + n, large);
		for(int i = 0;i < n;i++){
			cout << teams[i].t << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n;
  while(cin >> n){
    if(n==0) break;
    char t[n];
    char kt;
    int tt[2][n];
    int k;
    int h;
    for(int i=0;i<2;i++){
      for(int j=0;j<n;j++){
	tt[i][j] = 0;
      }
    }
    for(int i=0;i<n;i++){
      cin >> t[i];
      for(int j=0;j<n-1;j++){
	cin >> h;
	if(h==0) tt[0][i] += 1;
	if(h==1) tt[1][i] += 1;
      }
    }
    for(int i=n-1;i>0;i--){
      for(int j=0;j<i;j++){
	if(tt[0][j]<tt[0][j+1]){
	  k = tt[0][j];
	  tt[0][j] = tt[0][j+1];
	  tt[0][j+1] = k;
	  k = tt[1][j];
	  tt[1][j] = tt[1][j+1];
	  tt[1][j+1] = k;
	  kt = t[j];
	  t[j] = t[j+1];
	  t[j+1] = kt;
	}
	else if(tt[0][j]==tt[0][j+1]&&tt[1][j]>tt[1][j+1]){
    	  k = tt[0][j];
	  tt[0][j] = tt[0][j+1];
	  tt[0][j+1] = k;
	  k = tt[1][j];
	  tt[1][j] = tt[1][j+1];
	  tt[1][j+1] = k;
	  kt = t[j];
	  t[j] = t[j+1];
	  t[j+1] = kt;
	}
      }
    }
    for(int i=0;i<n;i++) cout << t[i] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
#include<queue>

using namespace std;

priority_queue<pair<pair<pair<int, int>,int>,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(make_pair(win,-lose),-i),name));
		}
		while(!results.empty()) {
			cout << results.top().second << endl;
			results.pop();
		}
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int main()
{
        while(true){
                int n;
                char t;
                int r;
                char x[10];
                cin >> n;
                if(n==0)
                        break;
                int win[10],lose[10];
                for(int i=0 ;i<10 ;i++){
                                win[i] = 0;
                                lose[i] = 0;
                }
                for(int i=0 ;i<n ;i++){
                        cin >> t;

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

                        }
                        x[i] = t;
                }
                char y=0,z=0;
                for(int i=0 ;i<n-1 ;i++){
                                for(int j=i+1 ;j<n ;j++){
                                if(win[i] < win[j]){
                                        z=win[i];
                                        win[i]=win[j];
                                        win[j]=z;
                                        y=x[i];
                                        x[i]=x[j];
                                        x[j]=y;
                                }
                                else if(win[i] == win[j]){
                                        if(lose[i] > lose[j]){
                                                z=win[i];
                                                win[i]=win[j];
                                                win[j]=z;
                                                y=x[i];
                                                x[i]=x[j];
                                                x[j]=y;
                                        }
                                }
								else if(win[i] == win[j] && lose[i] == lose[j]){
									if(x[i] > x[j]){
										y=x[i];
                                        x[i]=x[j];
                                        x[j]=y;
									}
								}
                        }
                }

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