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>
using namespace std;

struct Team {

	char name;
	int  winCnt;
	int  loseCnt;

};

int main()
{

	int n, r;
	Team t[10];

	while( cin >> n, n ) {

		for( int i = 0; i < 10; i++ ) { t[i].winCnt = 0; t[i].loseCnt = 0; }

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

				cin >> r;
				if( r == 0 ) t[i].winCnt++;
				else if( r == 1 ) t[i].loseCnt++;

			}
		}

		for( int i = 0; i < n - 1; i++ ) {
			for( int j = i + 1; j < n; j++ ) {

				Team temp;

				if( t[i].winCnt == t[j].winCnt ) {
					if( t[i].loseCnt == t[j].loseCnt ) continue;
					if( t[i].loseCnt > t[j].loseCnt ) {

						temp = t[i];
						t[i] = t[j];
						t[j] = temp;

					}
				}
				else if( t[i].winCnt < t[j].winCnt ) {

					temp = t[i];
					t[i] = t[j];
					t[j] = temp;

				}

			}
		}

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

	}

	return 0;

}

C++: #include<stdio.h>
int main(void)
{
    int n,r[30],i,j,w,m[30],l[30],a[11];
    char t[11],k;
    while(scanf("%d",&n),n!=0){
        for(i=0;i<n;i++){
            m[i]=0;
            l[i]=0;
            scanf(" %c",&t[i]);
            a[i]=i;
            for(j=0;j<n-1;j++){
                scanf("%d",&r[j]);
                if(r[j]==0){
                    m[i]++;
                }
                else if(r[j]==1){
                    l[i]++;
                }
            }
        }
        for(i=0;i<n-1;i++){
            for(j=i+1;j<n;j++){
                if(m[i]<m[j]){
                    w=m[i];
                    m[i]=m[j];
                    m[j]=w;
                    w=l[i];
                    l[i]=l[j];
                    l[j]=w;
                    k=t[i];
                    t[i]=t[j];
                    t[j]=k;
                    w=a[i];
                    a[i]=a[j];
                    a[j]=w;
                }
                else if(m[i]==m[j]){
                    if(l[i]>l[j]){
                        w=m[i];
                        m[i]=m[j];
                        m[j]=w;
                        w=l[i];
                        l[i]=l[j];
                        l[j]=w;
                        k=t[i];
                        t[i]=t[j];
                        t[j]=k;
                        w=a[i];
                        a[i]=a[j];
                        a[j]=w;
                    }
                    else if(l[i]==l[j]&&a[i]>a[j]){
                        w=m[i];
                        m[i]=m[j];
                        m[j]=w;
                        w=l[i];
                        l[i]=l[j];
                        l[j]=w;
                        k=t[i];
                        t[i]=t[j];
                        t[j]=k;
                        w=a[i];
                        a[i]=a[j];
                        a[j]=w;
                    }
                }
            }
        }
        for(i=0;i<n;i++){
            printf("%c\n",t[i]);
        }
    }
    return 0;
}

C++: #include<stdio.h>

#include<iostream>


using namespace std ;


pair< int ,int > p[ 10 ] ;



#define name first
#define lose second



int main()
{
	int n ;

	while( 1 )
	{
		scanf( "%d" ,&n ) ;


		if( n == 0 )
		{
			return 0 ;
		}



		int a ;

		char c[ 2 ] ;

		int i ,j ;
		for( i = 0 ; i < n ; ++i )
		{
			scanf( "%s" ,c ) ;


			p[ i ].name = c[ 0 ] * 100 ;


			for( j = 1 ; j < n ; ++j )
			{
				scanf( "%d" ,&a ) ;


				if( a < 2 )
				{
					if( a )
					{
						--p[ i ].lose ;
					}

					else
					{
						++p[ i ].name ;
					}
				}
			}
		}


		for( i = 0 ; i < n ; ++i )
		{
			for( j = i ; j < n ; ++j )
			{
				pair< int ,int > tmp ;

				if( p[ i ].name % 100 < p[ j ].name % 100 )
				{
					tmp = p[ i ] ;

					p[ i ] = p[ j ] ;

					p[ j ] = tmp ;
				}

				else if( p[ i ].lose < p[ j ].lose )
				{
					tmp = p[ i ] ;

					p[ i ] = p[ j ] ;

					p[ j ] = tmp ;
				}
			}
		}


		for( i = 0 ; i < n ; ++i )
		{
			printf( "%c\n" ,p[ i ].name / 100 ) ;
		}
	}
}

C++: #include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cstdlib>
#include <sstream>
#include <bitset>
#include <vector>
#include <cstdio>
#include <ctime>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <list>
#include <map>
#include <set>

using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back


int main() {
    int n;
    while(cin>>n){
        if(n==0)break;
        vector<pair<char,int> > a;
        rep(i,n){
            char t;
            cin>>t;
            
            int s=0;
            rep(j,n-1){
                int tr;
                cin>>tr;
                if(tr==0)s+=100;
                if(tr==1)s-=1;
            }
            a.pb(pii(t,s));
        }
        for(int i=0;i<n;i++){
            for(int j=n-1;j>i;j--){
                if( a[j-1].second < a[j].second )swap(a[j-1],a[j]);
            }
        }
        
        rep(i,n){
            cout<<a[i].first<<endl;
        }
    }

    return 0;
}