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

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

main {
  int n;
  while (cin >> n, n) {
    V<P<char, pii>> v(n);
    rep(i, n) {
      cin >> v[i].first;
      rep(_, n - 1) {
        int x;
        cin >> x;
        if (x != 2) (x ? v[i].second.second : v[i].second.first)++;
      }
    }
    stable_sort(all(v), [](auto &a, auto &b) {
      if (a.second.first != b.second.first) return a.second.first > b.second.first;
      return a.second.second < b.second.second;
    });
    for (auto &x : v) cout << x.first << endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define S second
#define F first
using namespace std;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef pair<string,PP> SPP;
bool cmp(const SPP &p,const SPP &q){
  if(p.S.F.F != q.S.F.F)return p.S.F.F > q.S.F.F;
  else if(p.S.F.S != q.S.F.S)return p.S.F.S < q.S.F.S;
  else return p.S.S.S < q.S.S.S;
}


int main(){
  int n;
  while(true){
    cin >> n;
    if(n == 0)break;
    vector<SPP> info; string s;
    for(int i=0;i<n;i++){
      cin >> s;
      info.push_back(SPP(s,PP(P(0,0),P(0,i))));
      for(int j=0;j<n-1;j++)
	{ int a;
	  cin >> a;
	  if(a == 0)info[i].S.F.F++;
	  else if(a == 1)info[i].S.F.S++;
	  else info[i].S.S.F++;
	}
    }
     
      sort(info.begin(),info.end(),cmp);
     
      for(int i=0;i<info.size();i++){
		cout << info[i].F << endl;
      }
    

  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct T{int w,l;string s;}t[10];
bool f(T l,T r){return l.w!=r.w?l.w>r.w:l.l<r.l;}
int main()
{
	int n,i,j,a;
	while(cin>>n,n)
	{
		for(i=0;i<n;++i)
		{
			cin>>t[i].s;
			t[i].w=t[i].l=0;
			for(j=1;j<n;++j)scanf("%d",&a),a==0?t[i].w++:(a==1?t[i].l++:0);
		}
		stable_sort(t,t+n,f);
		for(i=0;i<n;++i)cout<<t[i].s<<endl;
	}
	return 0;
}

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

struct E {
  int w;
  int l;
  char c;
  const bool operator<(const E &obj) const {
    if (w > obj.w) {
      return true;
    } else if (w < obj.w) {
      return false;
    } else {
      if (l < obj.l) {
        return true;
      } else {
        return false;
      }
    }
  }
};

int N;
E a[10];

bool input() {
  cin >> N;
  if (!N) {
    return false;
  }
  for (int k = 0; k < N; ++k) {
    E e;
    cin >> e.c;
    e.w = e.l = 0;
    for (int i = 0; i < N - 1; ++i) {
      int t;
      cin >> t;
      if (t == 0) {
        ++e.w;
      } else if (t == 1) {
        ++e.l;
      }
    }
    a[k] = e;
  }
  return true;
}

int main() {
  while (input()) {
    stable_sort(a, a + N);
    for (int i = 0; i < N; ++i) {
      cout << a[i].c << endl;
    }
  }
  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]||win[point]==win[j]&&low[point]>low[j])point=j;
cout<<str[point]<<endl;
win[point]=-1;
low[point]=11;
}
}

}

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

typedef struct
{
  string t;
  int id, w, l;
} Team;

class Solve
{
public:
  bool operator() (const Team& lhs, const Team& rhs) const
  {
    if (lhs.w == rhs.w) {
      if (lhs.l == rhs.l)
	return lhs.id < rhs.id;
      return lhs.l < rhs.l;
    }
    return lhs.w > rhs.w;
  }
};

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

    vector<Team> team(n);
    for (int i = 0; i < n; ++i) {
      team[i].id = i;
      cin >> team[i].t;
      for (int j = 0; j < n-1; ++j) {
	int r;
	cin >> r;
	if (r == 0)
	  ++team[i].w;
	else if (r == 1)
	  ++team[i].l;
      }
    }
    sort(team.begin(), team.end(), Solve());

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

  }
  return 0;
}