--TB36FDmn/VVEgNH/
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Here's my minimalist solution: 20 lines.  I believe it always gets
both rank and color-alternation correct.  If you only want to get
alternation correct *most* of the time, you can cut out another five
lines.  :-)

SUITS = %w{d c h s}.map {|s| s[0]}
CARDS = %w{A K Q J T 9}.map {|c| c[0]}

trump,hand = STDIN.readline, STDIN.readlines

puts trump
trump = SUITS.index(trump.downcase[0])

# If the suit after the trump suit is missing, we swap it with the
# other suit of the same color.  This ensures that we always have a
# correct color alternation when possible.
unless hand.find {|card| card[1] == SUITS[(trump+1)%4]}
  tmp = SUITS[(trump+1)%4]
  SUITS[(trump+1)%4] = SUITS[(trump+3)%4]
  SUITS[(trump+3)%4] = tmp
end

hand.map { |card| 
  suit = (SUITS.index(card[1]) - trump)%4
  num = CARDS.index(card[0])
  if num==3 && suit==2
    suit,num = 0,-1             # Left bower
  elsif num==3 && suit==0
    num = -2                    # Right bower
  end
  [suit,num,card.chomp]
}.sort.each{|c| puts "#{c[2]}\n" }


--TB36FDmn/VVEgNH/
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDgJmYnhUz11p9MSARAt1cAKC8/V15WvvXCnxPxtmkvOPHLS9KRwCbBu4G
9+kkX5s6DVtZP4uGzXHKX5gm
-----END PGP SIGNATURE-----

--TB36FDmn/VVEgNH/--