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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main( void )
{
	while ( 1 )
	{
		int n;
		cin >> n;
		if ( n == 0 )
			break;

		vector <int> id( n );
		vector <char> names( n );
		vector <int> wins( n, 0 );
		vector <int> loses( n, 0 );
		for ( int i = 0; i < n; i++ )
		{
			id[i] = i;
			cin >> names[i];

			for ( int j = 0; j < n - 1; j++ )
			{
				int s;
				cin >> s;
				if ( s == 0 )
					wins[i]++;
				else if ( s == 1 )
					loses[i]++;
			}
		}

		for ( int i = 0; i < n-1; i++ )
		{
			for ( int j = i+1; j < n; j++ )
			{
				if ( wins[i] < wins[j] )
				{
					swap( id[i], id[j] );
					swap( wins[i], wins[j] );
					swap( loses[i], loses[j] );
					swap( names[i], names[j] );
				}
				else if ( wins[i] == wins[j] )
				{
					if ( loses[i] > loses[j] )
					{
						swap( id[i], id[j] );
						swap( wins[i], wins[j] );
						swap( loses[i], loses[j] );
						swap( names[i], names[j] );
					}
					else if ( loses[i] == loses[j] )
					{
						if ( id[i] > id[j] )
						{
							swap( id[i], id[j] );
							swap( wins[i], wins[j] );
							swap( loses[i], loses[j] );
							swap( names[i], names[j] );
						}
					}
				}
			}
		}

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

	}
	return 0;
}

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

struct team {
    char name;
    int win, lose;
};

bool comp(team a, team b) {
    if (a.win > b.win) return true;
    else if (a.win < b.win) return false;
    return a.lose - b.lose;
}

int main() {
    while (true) {
        int n;
        cin >> n;
        if (!n) return 0;
        
        team t[n];
        for (int i = 0; i < n; i++) {
            t[i].win = 0;
            t[i].lose = 0;
        }

        for (int i = 0; i < n; i++) {
            cin >> t[i].name;
            for (int j = 0; j < n - 1; j++) {
                int r;
                cin >> r;
                if (r == 0) t[i].win++;
                else if (r == 1) t[i].lose++;
            }
        }
        sort(t, t + n, comp);
        for (int i = 0; i < n; i++) {
            cout << t[i].name << endl;
        }
    }
}

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

int main() {
	int n,i,j,g,w,l,a[10];
	char t[10];
	while(cin >>n) {
		if (n==0) break;
		for (i=0;i<n;i++) {
			cin >> t[i]; w=0; l=10;
			for (j=0; j<n-1 ; j++) { cin >> g; w+=(g==0); l-=(g==1);}
			a[i]=w*10000+l*100+(10-i);
		}
		sort(a,a+n,greater<int>());
		for (i=0;i<n;i++) cout << t[10-(a[i] % 100)] << 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<int, pair<int, char> > rank;
 
		int i,j;
		for (i = 0; i < n; ++i){
			getline (cin, str );
			char name;
			int in;
			int win = 0, lose = 0, draw = 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 (-win , make_pair (lose, name ) ) );
		} // end for
		
		multimap<int, pair <int, char> >::iterator it = rank.begin();
			
		for (; it != rank.end(); ++it ){
			cout << (*it).second.second << endl;
		} // end for
	} // end loop
		
	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) {
		map<char, int> m;
		vector<plc> v(n);

		for (int i = 0; i < n; i++) {
			cin >> v[i].second;
			m[v[i].second] = i;
			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;
		}

		for (int i = 0; i < n - 1; i++) {
			for (int j = i + 1; j < n; j++) {
				if (v[i].first < v[j].first)
					swap(v[j], v[i]);
			}
		}
		
		for (int i = 0; i < n - 1; i++) {
			for (int j = i + 1; j < n; j++) {
				if (v[i].first == v[j].first) {
					if (m[v[i].second] > m[v[j].second])
						swap(v[i], v[j]);
				}
			}
		}

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