I don't have any more insight about a head-to-head algorithm, but here's a better way of dealing with an arbitrary number of rule sets. Each rule is defined in a separate subroutine and an array dispatch table is used to evaluate them sequentially. If a rule's result is indeterminate, the next rule is tried. The following code is not rigorously tested, but should give you the idea.

my @teams = qw(mets yankees sox cubs braves cards); my @rules = ( \&wins, \&losses, \&headtohead, \&games, \&tiebreaker ); # games played, wins, losses, ties, tie breaker, teams defeated my %stats = ( braves => [20, 18, 2, 0, 1, {yankees => 1, cubs => 1, etc => 1} + ], yankees => [20, 18, 2, 0, 2, {braves => 1} ], mets => [20, 18, 2, 0, 3, {cubs => 1} ], sox => [20, 8, 12, 0, 4, {yankees => 1} ], cubs => [20, 8, 12, 0, 5, {cards => 1} ], cards => [20, 15, 5, 0, 6, {sox => 1} ] ); my @standing = sort{ my $cv; for (@rules){ $cv = $_->($a, $b); last if $cv; } return $cv; }@teams; print "@teams\n"; print "@standing\n"; ## The Rules: sub wins{ my ($a, $b) = @_; return $stats{$b}[1] <=> $stats{$a}[1]; } sub losses{ my ($a, $b) = @_; return $stats{$a}[2] <=> $stats{$b}[2]; } sub headtohead{ my ($a, $b) = @_; my $win = 0; $win-- if $stats{$a}[5]{$b}; $win++ if $stats{$b}[5]{$a}; return $win; } sub games{ my ($a, $b) = @_; return $stats{$a}[0] <=> $stats{$b}[0]; } sub tiebreaker{ my ($a, $b) = @_; return $stats{$a}[4] <=> $stats{$b}[4]; }

In reply to Re: Adding Special Rules to Sort by hangon
in thread 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.