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>
using namespace std;
#define MAX 10
class Team{
    public:
    char name;
    int id, win, defeat, draw;
    Team( int id=0, char name=' ', int win=0, int defeat=0, int draw=0 ): id(id), name(name), win(win), defeat(defeat), draw(draw){}
    bool operator < ( const Team &t ) const{
	if ( win == t.win ){
	    if ( defeat == t.defeat ) return id < t.id;
	    else return defeat < t.defeat;
	} else return win > t.win;
    }
};

main(){
    Team T[MAX];
    int n, x;
    while( cin >> n && n ){
	for ( int i = 0; i < n; i++ ) {
	    T[i] = i;
	    cin >> T[i].name;
	    T[i].defeat = T[i].win = T[i].draw = 0;
	    for ( int j = 0; j < n-1; j++ ){
		cin >> x;
		if ( x == 0 ) T[i].win++;
		else if ( x == 1 ) T[i].defeat++;
		else if ( x == 2 ) T[i].draw++;
	    }
	}
	sort( T, T + n );
	for ( int i = 0; i < n; i++ ){
	    cout << T[i].name << endl;
	}
    }
}

C++: #include <iostream>
using namespace std;
struct T {char s; int w, l;} t[10];
int main()
{
	int n, a, i, j, k;
	while (cin >> n, n)
	{
		for (i = 0; i < n; i++)
		{
			cin >> t[i].s;
			for (j = t[i].w = t[i].l = 0; j < n-1; j++)
			{
				cin >> a;
				if (a == 0)
					t[i].w++;
				if (a == 1)
					t[i].l++;
			}
		}
		for (i = 0; i < n-1; i++)
		{
			for (j = 0; j < n-1; j++)
			{
				if (t[j].w < t[j+1].w)
					swap(t[j], t[j+1]);
				else if (t[j].w == t[j+1].w && t[j].l > t[j+1].l)
					swap(t[j], t[j+1]);
			}
		}
		for (i = 0; i < n; i++)
			cout << t[i].s << endl;
	}
}

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

int n;
class team {
public:
	string name;
	int win, lose;
	team()
		: win(0), lose(0)
	{
	}
};
bool teamComp(const team& x, const team& y) {
	if (x.win > y.win) return true;
	else if (x.win == y.win && x.lose < y.lose) return true;
	return false;
}

int main () {
	while (cin >> n, n) {
		team teams[n];
		for (int i = 0; i < n; i++) {
			cin >> teams[i].name;
			for (int j = 0; j < n - 1; j++) {
				int x;
				cin >> x;
				switch (x) {
				case 0:
					teams[i].win++;
					break;
				case 1:
					teams[i].lose++;
					break;
				}
			}
		}
		stable_sort(teams, teams + n, teamComp);
		for(team* it = teams; it != teams + n; it++) {
			cout << it->name << endl;
		}
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct DATA{
  char Team;
  int Win;
  int Lose;
};

bool compare(const DATA&, const DATA&);

int main(){
  int i, j, n, score, win, lose;
  char c;
  DATA one;
  vector<DATA> data;

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

    for(i=0; i<n; ++i){
      cin >> c;

      win = 0;
      lose = 0;
      for(j=0; j<n-1; ++j){
	cin >> score;
	if(score == 0) win++;
	else if(score == 1) lose++;
      }
      one.Team = c;
      one.Win = win;
      one.Lose = lose;
      data.push_back(one);
    }

    sort(data.begin(), data.end(), compare);
    for(i=0; i != data.size(); ++i)
      cout << data[i].Team << endl;
    data.clear();
  }

  return 0;
}

bool compare(const DATA& x, const DATA& y){
  if(x.Win == y.Win)
    return x.Lose < y.Lose;
  else
    return x.Win > y.Win;
}

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

int main(){
  pair<int,char> game[12];
  int team,point[12][12],ten[12];
  
  while(1){
    cin >> team;
    if(team == 0)break;
    for(int i=0;i<12;i++){
      ten[i] = 0;
    }
        
    for(int i=0;i<team;i++){
      cin >> game[i].second;
      for(int j=0;j<team-1;j++){
	cin >> point[i][j];
	if(point[i][j] == 0){
	  ten[i] += 2;
	}
	else if(point[i][j] == 2){
	  ten[i]++;
	}
      }
      game[i].first = ten[i];
    }
    sort(game,game+team);

    for(int i=team-1;0<=i;i--){
      cout << game[i].second << endl;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(void)
{
	int i,j,k,n,sum[11];
	int data;
	char name[11][11];
	while(1) {
		cin>>n;
		if(!n) break;
		for(i=0;i<n;i++) sum[i]=0;
		for(i=0;i<n;i++) {
			cin>>name[i];
			for(j=0;j<n-1;j++) {
				cin>>data;
				if(data==0) sum[i]+=10000;
				else if(data==1) sum[i]-=100;
			}
		}
		for(i=0;i<n-1;i++) {
			for(j=i+1;j<n;j++) {
				if(sum[i]<sum[j]) {
					swap(sum[i],sum[j]);
					swap(name[i],name[j]);
				}
			}
		}
		for(i=0;i<n;i++) cout<<name[i]<<endl;
	}
	return 0;
}

C++: 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, char> plc;


int main()
{
	int n; 
	while (cin >> n, n) {
		vector<plc> v(n);

		for (int i = 0; i < n; i++) {
			cin >> v[i].second;
			int num = 0;
			for (int j = 0; j < n - 1; j++) {
				int x; cin >> x;
				if (x == 0)num += 100000;
				if (x == 1)num--;
			}
			v[i].first = num;
		}

		sort(v.begin(), v.end());
		reverse(v.begin(), v.end());

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