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
	}
	draw := func(c1, c2 *Team) bool {
		return c1.Draw < c2.Draw
	}

	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<bits/stdc++.h>
using namespace std;
long long int sum[11];
int main(void)
{
	int i,j,k,n;
	int data;
	string name[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]+=1000000;
				else if(data==1) sum[i]-=1;
			}
		}
		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 <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
int w[15],l[15];
int a[15];
bool cmp(const int &a,const int &b)
{
	if(w[a] != w[b]) return w[a] > w[b];
	if(l[a] != l[b]) return l[a] < l[b];
	return a < b;
}
int main()
{
	while(1)
	{
		int n;
		cin >> n;
		if(n == 0) return 0;
		char h[15];
		fill(w,w+15,0);
		fill(l,l+15,0);
		for(int i=0;i<15;i++) a[i] = i;
		for(int i=0;i<n;i++)
		{
			scanf(" %c",&h[i]);
			for(int j=1;j<n;j++)
			{
				int x; cin >> x;
				if(x == 0) w[i]++;
				else if(x == 1) l[i]++;
			}
		}
		sort(a,a+n,cmp);
		for(int i=0;i<n;i++) cout << h[a[i]] << endl;
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
typedef pair<pair<int, int>, char> p;
int main() {
	int n; 
	while (cin >> n, n) {
		priority_queue<p>pq;
		for (int i = 0; i < n; i++) {
			p p1;
			cin >> p1.second;
			p1.first.first = 0;
			p1.first.second = n;
			for (int j = 1; j < n; j++) {
				int k; cin >> k;
				if (!k)p1.first.first++;
				else if (k == 1)p1.first.second--;
			}
			pq.push(p1);
		}
		for (int i = 0; i < n; i++) {
			cout << pq.top().second << endl;
			pq.pop();
		}
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
long long int i,j,k,n,sum[9],data;
int main(void)
{

	char name[10];
	while(1) {
		cin>>n;
		if(n==0) 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]+=100000000;
				else if(data==1) sum[i]-=1;
			}
		}
		
		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<iostream>
#include<vector>
using namespace std;
int main() {
	while (1) {
		int n;
		cin >> n;
		if (n == 0)
			break;
		
		vector<char> t(n);
		vector<int> win(n), lose(n), drow(n);
		vector<int> num(n);
		for (int i = 0; i < n; i++)
			num.at(i) = i;

		int score;
		for (int i = 0; i < n; i++) {
			cin >> t.at(i);
			for (int j = 0; j < n - 1; j++) {
				cin >> score;
				switch (score) {
				case(0):win.at(i)++;
				case(1):lose.at(i)++;
				case(2):drow.at(i)++;
				}
			}
		}

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

		for (int i = 0; i < n; i++)
			cout << t.at(num.at(i)) << endl;
	}
}

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

bool compare(pair< string, pair< int, int > > a, pair< string, pair< int, int > > b){
	if(a.second.first != b.second.first){
		return a.second.first > b.second.first;
	}
	
	return a.second.second < b.second.second;
}

int main(){
	int n;
	vector< pair< string, pair< int, int > > > team;
	
	while(cin >> n, n){
		team.clear();
		team.resize(n);
		
		string str;
		int s, w, l;
		
		for(int i = 0; i < n; ++i){
			cin >> str;
			w = l = 0;
			for(int j = 0; j < n - 1; ++j){
				cin >> s;
				if(s == 0) ++w;
				else if(s == 1) ++l;
			}
			
			team[i] = make_pair< string, pair< int, int > >(str, make_pair< int, int >(w, l) );
		}
		
		sort(team.begin(), team.end(), compare);
		
		for(vector< pair< string, pair< int, int > > >::iterator it = team.begin(); it != team.end(); ++it){
			cout << it->first << endl;
		}
		
	}
	
	return 0;
}

C++: #include <iostream>
#include <map>
#include <sstream>
//#include <fstream>

using namespace std;

int main()
{
//	cut here before submit 
//	freopen("testcase.baseballchamp", "r", stdin);
	string str = "";

	while (getline (cin, str ) ){
		int n;
		stringstream ssn(str);
		ssn >> n;
		
		if (n == 0 ){
			break;
		} // end if
		
		multimap< pair<int,int>, char > rank;
 
		int i,j;
		for (i = 0; i < n; ++i){
			getline (cin, str );
			char name;
			int in;
			int win = 0, lose = 0;
			
			stringstream ss(str);
			ss >> name;
			
			for (j = 0; j < n - 1; ++j){
				ss >> in;
				switch (in ){
					case 0: ++win; break;
					case 1: ++lose; break;
//					case 2: ++draw; break;
				} // end switch
			} // end for
			rank.insert (make_pair (make_pair (-win, lose ),name ) );
		} // end for
		
		multimap<pair <int, int> ,char >::iterator it = rank.begin();
			
		for (; it != rank.end(); ++it ){
//			cout << (*it).first.first << ' ' << (*it).first.second << ' ';
			cout << (*it).second << endl;
		} // end for
	} // end loop
		
	return 0;
}