I am attempting to use Perl to sort a group of sports teams using a number of criteria. The criteria are as follows:

1) Winning percentage (Highest to Lowest)
2) Number of Wins (Highest to Lowest)
3) Head to Head (if team A beat team B, then team A should be ahead of team B)
4) Strength of schedule rating (Highest to Lowest)
5) Number of wins of some specific various types
6) Tiebreaker Number (highest to lowest, a set value assigned to each team at the beginning of the program, is different for each team)

Using the built in sort allows me to do everything I want except deal with criteria 3 correctly. It is possible to have more then two teams with the same criteria 1 and 2. Those teams may or may not have played each other, so I could have:

teamA beat teamB
teamB beat teamC
teamC beat teamA

In this scenario I'd want to skip criteria 3, since no team has a clear advantage over all the others.

I could also have:

teamA beat teamB and teamC
teamB beat teamC

In that scenario criteria 3 needs to rank the teams as A, B, C

The most confusing possibility is:

teamA beat teamB and teamC
teamB beat teamC
teamD did not play teamA, teamB or teamC.

In that scenario I'd want the teams ranked by criteria 4, 5 and 6, but with the caveat that teamA has to be ahead of (but not necesarily directly next to) teamB and teamC, and teamB has to be ahead of teamC. So depending on criteria 4, 5 and 6, any of the following would be valid results:

teamA, teamB, teamC, teamD
teamA, teamD, teamB, teamC
teamA, teamB, teamD, teamC
teamD, teamA, teamB, teamC

So basically what I need to do is define an undertimined number of special rules that say "this team has to be ahead of (but not necessarily directly ahead of) this other team", where a specific "this team" or "this other team" could appear in more then one such rule.

Following is the current sort subroutine I'm using which doesn't handle all these scenarios:
sub rank_teams { if($wp{$a} < $wp{$b}) { return 1; } if($wp{$a} > $wp{$b}) { return -1; } if($wins{$a} > $wins{$b}) { $tiebreaker{$a} .= "Most Wins v. $b; "; re +turn -1; } if($wins{$a} < $wins{$b}) { $tiebreaker{$b} .= "Most Wins v. $a; "; re +turn 1; } if($wv{"a*b"} > $lv{"a*b"}) { $tiebreaker{$a} .= "Head-to-Head ($wv-$l +v) v. $b; "; return -1; } if($wv{"a*b"} < $lv{"a*b"}) { $tiebreaker{$b} .= "Head-to-Head ($lv-$w +v) v. $a; "; return 1; } if($sp{$a} > $sp{$b}) { $tiebreaker{$a} .= "Pts. v. $b; "; return -1; +} if($sp{$a} < $sp{$b}) { $tiebreaker{$b} .= "Pts. v. $a; "; return 1; } if($ll_wins{$a} > $ll_wins{$b}) { $tiebreaker{$a} .= "LL Wins ($ll +_wins{$a}-$ll_wins{$b}) v. $b; "; return -1; } if($ll_wins{$a} < $ll_wins{$b}) { $tiebreaker{$b} .= "LL Wins ($ll +_wins{$b}-$ll_wins{$a}) v. $a; "; return 1; } if($bbbDivision{$a} ne "LL") { if($l_wins{$a} > $l_wins{$b}) { $tiebreaker{$a} .= "L Wins ($l_win +s{$a}-$l_wins{$b}) v. $b; "; return -1; } if($l_wins{$a} < $l_wins{$b}) { $tiebreaker{$b} .= "L Wins ($l_win +s{$b}-$l_wins{$a}) v. $a; "; return 1; } } if($bbbDivision{$a} ne "LL"&& $bbbDivision{$a} ne "L") { if($m_wins{$a} > $m_wins{$b}) { $tiebreaker{$a} .= "M Wins ($m_win +s{$a}-$m_wins{$b}) v. $b; "; return -1; } if($m_wins{$a} < $m_wins{$b}) { $tiebreaker{$b} .= "M Wins ($m_win +s{$b}-$m_wins{$a}) v. $a; "; return 1; } } if($bbbDivision{$a} eq "S") { if($s_wins{$a} > $s_wins{$b}) { $tiebreaker{$a} .= "S Wins ($s_win +s{$a}-$s_wins{$b}) v. $b; "; return -1; } if($s_wins{$a} < $s_wins{$b}) { $tiebreaker{$b} .= "S Wins ($s_win +s{$b}-$s_wins{$a}) v. $a; "; return 1; } } if($tb_num{$a} > $tb_num{$b}) { $tiebreaker{$a} .= "By Lot v. $b; "; r +eturn -1; } if($tb_num{$a} < $tb_num{$b}) { $tiebreaker{$b} .= "By Lot v. $a; "; r +eturn 1; } return 1; }

In reply to Adding Special Rules to Sort by ctfishman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.