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<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
int main() {
	int n;
	while (cin >> n&&n != 0) {
		char a[20];
		int b[20][2];
		bool l[20];
		for (int i = 0; i < n; i++) {
			cin >> a[i];
			b[i][0] = 0;
			b[i][1] = 0;
			l[i] = false;
			for (int j = 0; j < n - 1; j++) {
				int x;
				cin >> x;
				if (x == 0) b[i][1]++;
				else if (x == 1) b[i][0]++;
			}
		}
		for (int i = 0; i < n; i++) {
			int maxw = 100000, minl = 10000,t=0;
			for (int j = n-1; j >= 0; j--){
				if (!l[j]) {
					if (maxw > b[j][0]) {
						t = j;
						maxw = b[j][0];
					}
					else if (maxw == b[j][0]) {
						if (b[t][1] <= b[j][1]) {
							t = j;
						}
					}
				}
			}
			l[t] = true;
			cout << a[t] << endl;
			maxw = -1;
		}
	}
}

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

using namespace std;

int main(){
  int n;
  while(cin >> n, n!=0){
    vector<pair<int, char>> v;

    for(int i=0; i<n; ++i){
      char tmp;
      cin >> tmp;
      v.push_back(make_pair(0, tmp));
      
      for(int j=0; j<n-1; ++j){
	int scr;
	cin >> scr;
	
	if(scr==0){
	  v[i].first = v[i].first + 10;
	}else if(scr==2){
	  v[i].first = v[i].first + 1;
	}
      }
      
    }
    sort(v.begin(), v.end(), greater<pair<int, char>>());
    for(auto i: v){
      cout << i.second << endl;
    }
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
typedef pair<int,int> P;
typedef pair<P,string> PP;
int n,m;
PP t[10];
int main(){
  while(cin>>n&&n){
    for(int i=0;i<n;i++){
      cin>>t[i].second;
      t[i].first.first=t[i].first.second=0;
      for(int j=0;j<n;j++){
	if(i==j)continue;
	cin>>m;
	if(m==0)t[i].first.first--;
	else if(m==1)t[i].first.second++;
      }
    }
    sort(t,t+n);
    for(int i=0;i<n;i++){cout<<t[i].second<<endl; 
  }
  return 0;
}

C++: #include <iostream>
#include <cstdlib>

using namespace std;

typedef struct r {

  char team;
  int win;
  int lose;

} record;

int comp(const void* c1,const void* c2) {

  r t1=*(r*)c1;
  r t2=*(r*)c2;

  if(t1.win==t2.win) return t1.lose-t2.lose;

  else return t2.win-t1.win;

}

int main() {

  int n;
  char t;

  record rec[10];

  int temp;


  while(cin >> n) {

    if(n==0) break;

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

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

	cin >> temp;

	switch(temp) {

	case 0:
	  rec[i].win++;
	  break;

	case 1:
	  rec[i].lose++;
	  break;

	}

      }

      //cout << rec[i].team << " " << rec[i].win << " " << rec[i].lose << endl;

    }

    qsort(rec,n,sizeof(r),comp);

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

  }

  return 0;

}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

void ins(vector< pair<int,pair<int,char> > > &a){
    int s = a.size() - 1;
    if(s == 0) return ;
    while(a[s].first < a[s-1].first){
	swap(a[s],a[s-1]);
	s--;
	if(s==0) break;
    }
    if(s ==0) return;
    while(a[s].first == a[s-1].first && a[s].second.first > a[s-1].second.first){
	swap(a[s],a[s-1]);
	s--;
	if(s==0) break;
    }
    return ;
}

int main(){
    while(true){
	int n;
	cin >> n;
	vector< pair<int,pair<int,char> > > ans;
	if(n == 0) break;
	REP(i,n){
	    char t;
	    int temp;
	    int w,l;
	    w =0 ;
	    l = 0;
	    cin >> t;
	    REP(i,n-1) {
		cin >> temp;
		if(temp == 0) w++;
		else if( temp == 1) l++;
	    }
	    ans.PB( pair<int,pair<int,char> >( w , pair<int,char>(l,t) ));
	    ins(ans);
	}
	REP(i,n) cout << ans[i].second.second << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int n,r;pair<int,char>d[10];
int main(){
	  while (true) {
       cin>>n;
        if (n == 0)break;
        for (int i = 0; i < n; i++) {
            cin >> d[i].second; d[i].first = i;
            for (int j = 0; j < n - 1; j++) {
				cin>>r;
                if (r == 0) d[i].first -= 100;
                if (r == 1) d[i].first += 10;
            }
        }
        sort(d, d + n);
        for (int i = 0; i < n; i++) cout<<d[i].second<<endl;
    }

	return 0;
}

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


using namespace std;
struct node{
  char name;
  int win,lost;
  node(char n,int w, int l): name(n),win(w), lost(l){}
  bool operator <(const node &e) const{
    if(win == e.win)
	return lost < e.lost;
    else
      return win > e.win;
  }

  bool operator >(const node &e) const{
    if(win == e.win)
      return lost > e.lost;
    else
      return win < e.win;
      }
};


int main()
{
  int n;
  for(;cin>>n,n;)
    {
      vector<node> data;
      for(int i=0;i<n;i++)
	{
	  char t;
	  cin>>t;
	  int win=0;
	  int lost=0;
	  for(int i=1;i<n;i++)
	    {
	      int tmp;
	      cin>>tmp;
	      if(tmp==0)
		win++;
	      else if(tmp==1)
		lost++;
	    }
	  data.push_back(node(t,win,lost));
	}
      sort(data.begin(),data.end());
      for(int i=0;i<n;i++)
	cout<<data[i].name<<endl;
    }
  
}

C++: #include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
	while (1) {
		int n;
		cin >> n;
		if (n == 0) return 0;
		vector<pair<string, int>>v(n);
		for (int i = 0; i < n; i++) {
			cin >> v[i].first;
			for (int j = 0; j < n - 1; j++) {
				int num;
				cin >> num;
				if (num == 0) v[i].second += 100;
				if (num == 1) v[i].second -= 1;
			}
		}
		for (int i = 0; i < n; i++) {
			for (int j = n - 1; j >= i + 1; j--) {
				if (v[j].second < v[j - 1].second) swap(v[j], v[j - 1]);
			}
		}
		for (int i = n - 1; i >= 0; i--) {
			cout << v[i].first << endl;
		}
	}
	return 0;
}