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, draw).Sort(teams)
		for _, t := range teams {
			fmt.Println(t.Name)
		}
	}
}

C++: #include<iostream>

char *team;
int n,**res,wl;
bool* flag;

void display(){
	int count=0;
	flag = new bool[n];
	int tmp = 0;
	for(int i=0; i<n; i++)
		flag[i] = false;
	for(int i=0; i<n; i++)
		if(flag[i])
			std::cout << i << std::endl;
	while(true){
		for(int i=0; i<n; i++){
			if(flag[i] == true)
				continue;
			if(res[tmp][0] < res[i][0])
				tmp = i;
			else if(res[tmp][0] == res[i][0] && res[tmp][1] > res[i][1])
				tmp = i;
		}
		std::cout << team[tmp] << std::endl;
		count++;
		if(count == n)
			return;
		flag[tmp] = true;
		tmp = 0;
		while(flag[tmp]){
			tmp++;
			if(tmp == n)
				tmp = 0;
		}
	}
}

int main(){
	while(true){
		std::cin >> n;
		if(n==0)
			return 0;
		team = new char[n];
		res = new int*[n];
		for(int i=0; i<n; i++){
			res[i] = new int[3];
			for(int k=0; k<3; k++)
				res[i][k] = 0;
		}
		for(int i=0; i<n; i++){
			std::cin >> team[i];
			for(int k=0; k<n-1; k++){
				std::cin >> wl;
				res[i][wl]++;
			}
		}
		display();
		delete[] flag;
		for(int i=0; i<n; i++)
			delete[] res[i];
		delete[] res;
		delete[] team;
	}
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
int main(void)
{
	char name[10];
	long long int toku[9][5],n,make,kati,hiki,sum[9],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++;
			}
			sum[i]=kati*10000000+(make*-1);
		}
		for(int i=0;i<n;i++) touroku[i]=sum[i];
		sort(sum,sum+n,greater<int>());
		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 <cstdio>

using namespace std;
int i,j,make[100],kati[100];
char name[100];

void change(){
  int tmp1,tmp2;
  char tmp3;
  tmp1 = kati[j+1];
  kati[j+1] = kati[j];
  kati[j] = tmp1;
  
  tmp2 = make[j+1];
  make[j+1] = make[j];
  make[j] = tmp2;
  
  tmp3 = name[j+1];
  name[j+1] = name[j];
  name[j] = tmp3;
}

int main(){
  int n,s;
  while(1){
    cin >> n;
    if( n == 0 )break;
    for( i = 0;i < n ; i++){
      kati[i] = 0;
      make[i] = 0;
    }
    
    for( i = 0 ; i < n ; i++ ){
	cin >> name[i];
      for( j = 0; j < n-1 ; j++ ){
	cin >> s;
	if(s == 0)kati[i]++;
	if(s == 1)make[i]++;
      }
    }

    
    
    for( i = n-1;i >0 ; i--){
      for( j = 0; j < i ; j++){
	if(make[j] < make[j+1] ){
	  change();
	}
      }
    }
    
    for( i = n-1;i >0 ; i--){
      for( j = 0; j < i ; j++){
	if(kati[j] < kati[j+1] ){
	  change();
	}
      }
    }

    for(i = 0;i < n ;i++)cout << name[i] << endl;
    
  }
  return 0;
}

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

using namespace std;
int i,j,make[100],kati[100];
char name[100];

void change(){
  int tmp1,tmp2;
  char tmp3;
  tmp1 = kati[j+1];
  kati[j+1] = kati[j];
  kati[j] = tmp1;
  
  tmp2 = make[j+1];
  make[j+1] = make[j];
  make[j] = tmp2;
  
  tmp3 = name[j+1];
  name[j+1] = name[j];
  name[j] = tmp3;
}

int main(){
  int n,s;
  while(1){
    cin >> n;
    if( n == 0 )break;
    for( i = 0;i < 100 ; i++){
      kati[i] = 0;
      make[i] = 0;
    }
    
    for( i = 0 ; i < n ; i++ ){
	cin >> name[i];
      for( j = 0; j < n-1 ; j++ ){
	cin >> s;
	if(s == 0)kati[i]++;
	if(s == 1)make[i]++;
      }
    }

    
    
    for( i = n-1;i >0 ; i--){
      for( j = 0; j < i ; j++){
	if(make[j] > make[j+1] ){
	  change();
	}
      }
    }
    
    for( i = n-1;i >0 ; i--){
      for( j = 0; j < i ; j++){
	if(kati[j] < kati[j+1] ){
	  change();
	}
      }
    }

    for(i = 0;i < n ;i++)cout << name[i] << endl;
    
  }
  return 0;
}

C++: /*
 * src.cpp
 *
 *  Created on: 2016/07/27
 *      Author: joi
 */

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

const int MAX_N=10;

int N;

struct team{
	int w,l,index;
	char name;
	bool operator < (const team &a)const{
		if (w!=a.w){
			return w<a.w;
		}
		if (l!=a.l){
			return l>a.l;
		}
		return index>a.index;
	}
};

team t[MAX_N];

int main(){
	do{
		cin>>N;
		if (N){
			cin.ignore();
			REP(i,N){
				string s;
				getline(cin,s);
				t[i].w=t[i].l=0;
				t[i].index=i;
				t[i].name=s[0];
				REP(j,N-1){
					char r=s[j*2+2];
					if (r=='0'){
						t[i].w++;
					}else if (r=='1'){
						t[i].l++;
					}
				}
			}
			sort(t,t+N);
			REP(i,N){
				cout<<t[N-i-1].name<<endl;
			}
		}
	}while (N);
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,point,win[10],low[10];
    char str[10];
    while(cin>>n&&n!=0){
        for(int i=0;i<n;i++){
            win[i]=0;low[i]=0;
            cin>>str[i];
            for(int j=1;j<n;j++){
                cin>>point;
                if(point==0)win[i]++;
                else if(point==1)low[i]++;
            }
        }
        for(int i=0;i<n;i++){
            point=0;
            for(int j=1;j<n;j++)if(win[point]<=win[j]&&low[point]>low[j])point=j;
            cout<<str[point]<<endl;
            win[point]=-1;
            low[point]=20;
        }
    }

}

C++: #include<stdio.h>
int main(void)
{
	int a,s,d,f[10],g[10],j,i,k[10],z;
	char h[2][11];
	for(i=1;i<=10;i++){
		k[i]=0;
		g[i]=0;
		f[i]=0;
	}
	scanf("%d",&a);
	for(i=1;i<=a;i++){
		scanf("%s",h[i]);
		for(j=1;j<=a-1;j++){
			scanf("%d",&s);
			if(s==0){
				f[i]++;
			}
			if(s==1){
				g[i]++;
			}
		}
		k[i]=i;
	//	printf("%s %d\n",h[k[i]],k[i]);
	}
	for(i=1;i<=a;i++){
		for(j=i+1;j<=a;j++){
			if(f[i]<f[j]){
				z=f[i];
				f[i]=f[j];
				f[j]=z;
				z=g[i];
				g[i]=g[j];
				g[j]=z;
				z=k[i];
				k[i]=k[j];
				k[j]=z;
			}
			if(f[i]==f[j]){
				if(g[i]>g[j]){
					z=f[i];
					f[i]=f[j];
					f[j]=z;
					z=g[i];
					g[i]=g[j];
					g[j]=z;
					z=k[i];
					k[i]=k[j];
					k[j]=z;
				}
			}
		}
	}
	for(i=1;i<=a;i++){
		printf("%s\n",h[k[i]]);
	}
	return 0;
}