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<map>
#include<string>
#include<stdio.h>
#include<cstdio>
#include<math.h>
using namespace std;
int main(){
    int n,score,sum[10000],memo=0,max=-100;
    char ch[100],tem;
    while(1){
    cin>>n;
        if(n==0){
            break;
        }
    for(int i=0;i<n;i++){
        cin>>ch[i];
        for(int j=0;j<n-1;j++){
            cin>>score;
            if(score==0){
                memo+=1;
            }
            else if(score==1){
                memo-=1;
            }
        }
        sum[i]=memo;
        memo=0;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(sum[j]>max){
                max=sum[j];
                tem=ch[j];
                memo=j;
            }
        }
        cout<<tem<<endl;
        sum[memo]=-100;
        max=-100;
    }
    }
    return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>

using namespace std;
long long int sum[9];
int main(void)
{
	char name[10];
	int toku[9][5],n,make,kati,hiki,touroku[9];
	int flg[9];
	while(1) {
		cin>>n;
		if(n==0) break;
		for(int i=0;i<n;i++) {
			cin>>name[i];
			for(int j=0;j<n-1;j++) {
				cin>>toku[i][j];
			}
		}
		for(int i=0;i<n;i++) {sum[i]=0;touroku[i]=0;}
		for(int i=0;i<n;i++) {
			kati=0;make=0;
			for(int j=0;j<n-1;j++) {
				if(toku[i][j]==0) kati++;
				if(toku[i][j]==1) make++;
			}	
		//	if(kati>0)
				sum[i]=kati*100000000+(make*-1);
	/*		else 
				sum[i]=kati*100000000+(make*-1);*/
		/*	else if(kati==0 && make>0) 
				sum[i]=kati*10000000*-1+(make*-1);*/
		//	cout<<sum[i]<<" "<<name[i]<<endl;
		}
		for(int i=0;i<n;i++) touroku[i]=sum[i];
		sort(sum,sum+n,greater<int>());
	//	for(int i=0;i<n;i++) cout<<sum[i]<<endl;;
		for(int i=0;i<n;i++) flg[i]=0;
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				if(sum[i]==touroku[j] && flg[j]==0) {
					cout<<name[j]<<endl;
					flg[j]=1;
					break;
				}
			}
		}
	}
}

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 ( i > 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 <vector>
#include <algorithm>
using namespace std;

class Team
{
  public:
    Team(char name, const vector<int>& scores, int index);

    bool operator<(const Team& right) const;
    char Name() const;

  private:
    char m_name;
    int m_win;
    int m_lose;
    int m_index;
};

Team::Team(char name, const vector<int>& scores, int index) :
  m_name(name), m_win(0), m_lose(0), m_index(index)
{
  for (auto score : scores)
  {
    switch(score)
    {
      case 0:
        m_win++;
        break;
      case 1:
        m_lose++;
        break;
    }
  }
}

bool Team::operator<(const Team& right) const
{
  return m_win == right.m_win ? ( m_lose == right.m_lose ? m_index > right.m_index : m_lose > right.m_lose ) : m_win < right.m_win;
}

char Team::Name() const
{
  return m_name;
}

int main(void)
{
  while (true)
  {
    int n;
    cin >> n;
    if (n == 0) break;

    vector<Team> teams;
    for (int i = 0; i < n; ++i)
    {
      char name;
      cin >> name;
      vector<int> scores;
      for (int j = 0, buf; j < n - 1; ++j)
      {
        cin >> buf;
        scores.push_back(buf);
      }
      teams.push_back(Team(name, scores, i) );
    }

    sort(teams.rbegin(), teams.rend() );
    for (auto t : teams)
    {
      cout << t.Name() << endl;
    }
  }
  return 0;
}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

struct team
{
	string id;
	int win;
	int lose;

	team(string s,int w,int l) : id(s),win(w),lose(l) {}

	bool operator < (const team& t) const{
		return win == t.win ? lose < t.lose : win > t.win;
	}
};

int main()
{
	int n;
	while(cin >> n && n)
	{
		vector<team> v;

		rep(i,n)
		{
			string s;
			cin >> s;
			
			int w = 0;
			int l = 0;
			
			rep(j,n-1)
			{
				int temp;
				cin >> temp;

				if(temp == 0) w++;
				else if(temp == 1) l++;
			}

			v.push_back(team(s,w,l));
		}

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


		rep(i,v.size())
		{
			cout << v[i].id <<  endl;
		}
	}
	return 0;
}

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

int main() {
  int n;
  while(cin >> n && n) {
    multimap<int,string> ans;
    for(int i=0; i<n; i++) {
      string name;
      cin >> name;
      int x,rank=500;
      for(int j=0; j<n-1; j++) {
	cin >> x;
	if(x==0) {
	  rank+=1000;
	} else if(x==1) {
	  rank--;
	}
      }
      string t=name;
      ans.insert(make_pair(rank,t));
    }
    vector<string> a;
    int d[n],k=-1,m=-1;
    fill(d,d+n,0);

    for(multimap<int,string>::reverse_iterator it=ans.rbegin(); it!=ans.rend(); it++) {
      if(m!=it->first) {
	k++;
	m=it->first;
      }
      d[k]++;
      a.push_back(it->second);
    }
    k=0;
    for(int i=0; i<n; i++) {
      for(int j=i+d[k]-1; j>=i; j--) cout << a[j] << endl;
      i+=d[k]-1;
      k++;
    }
  }
  return 0;
}