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
	}

	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 ( 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;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF (1<<21)

bool cmp(const pair<int,char> &a,const pair<int,char> &b){
	return a.first > b.first;
}
int main(){
	int n;
	while(cin >> n,n){
		vector< pair<int,char> > data(n);
		rep(i,n){
			int sum = 0;
			cin >> data[i].second;
			rep(j,n-1){
				int t;
				cin >> t;
				sum += (t==0?10:t==2?1:0);
			}
			data[i].first = sum;
		}
		stable_sort(data.begin(),data.end(),cmp);
		rep(i,n){
			cout << data[i].second << endl;	
		}
	}
}

C++: #include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int n,r,team[10]={0},t;
	char name[10],T;
	while(1){
		for(int i=0;i<10;i++) team[i]=0;
		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 >> r;
				if(r==0)
					team[i] = team[i] + 2;
				else if(r==2)
					team[i] = team[i] + 1;
			}
		}
		for(int i=0;i<n-1;i++){
			for(int j=i+1;j<n;j++){
				if(team[i] < team[j]){
					t = team[i];
					team[i] = team[j];
					team[j] = t;
					T = name[i];
					name[i] = name[j];
					name[j] = T;
				}
			}
		}
		for(int i=0;i<n;i++){
			cout << name[i] << endl;
		}
	}
	return 0;
}