How to compare contingency tables for a specific pattern? I have a a series of contingency tables, like:
    A   B 
  |----|----|
C | 0  | 10 |
D | 17 | 2  |

and I need a numerical value to select and order those tables where:


*

*(C,A) tends to 0, is much smaller than (C,B)

*(D,B) tends to 0, is much smaller than (D,A)

*(C,B) and (D,A) are > 0, the bigger the better


It doesn't matter if the total of the first row is bigger than the total of the second row.
Example: this table
    A   B 
  |----|----|
C | 12 | 41 |
D | 72 | 0  |

is good, and this table
    A   B 
  |-----|-----|
C | 178 | 100 |
D | 266 |   1 |

is still OK-ish, but should be ranked lower than the other one. The first row is clearly bad, but the second row makes up for it.
EDIT:
After John's answer, I've plotted the data ordered by three "measures of fit", to compare my two measures against John's.


*

*Inverse of Matthews Correlation Coefficient (MCC): a "perfect table" as defined above would produce an MCC of -1, while switching columns would provide "1". So doing -MCC and filtering out all values < 0 gives a good filtering/sorting index, and scales nicely between [0, 1].

*Error Rate Difference: proportion of (C,B) on the first row minus proportion of (D,B) on the second row. A value of 1 means that (C,A) and (D,B) are equal to 0. Scales nicely between [0, 1].

*John's fitting expression: doesn't fit into [0, 1], but models the constraints literally.


Here are the plots:



 A: Your original suggestion to use p-values was problematic in a number of ways. The biggest one is probably that they're dependent upon N such that a table with a small number of values but very well fit what you desire would have a higher p-value than one that had more values but didn't fit nearly so well.
How about just using the requirements you set out as the measure? The function below just turns your list of requirements into a simple equation. The numerator can be larger than the denominator but only when the requirements are strongly met. I don't think this is going to be too dependent on N but you should try and see what it looks like. Check the distribution of values and see if it's reasonable.
You might also note that I flattened your matrices. The indexes I used will match the matrices and this will function with a matrix or a vector. So if you already have all of the tables as matrices this would work just fine.
good <- c(12, 72, 41, 0)
ok <- c(178, 266, 100, 1)
bad <- c(178, 26, 100, 40)

myStat <- function(y){
    ( (y[3] - y[1]) +  (y[2] - y[4]) + y[2] + y[3] ) / sum(y)
    }

myStat(ok)
myStat(good)
myStat(bad)

When I generate some random binomial data I get a normal distribution centred on 0.5 which makes sense if you look at the equation carefully. If your criteria are correct high scores will be "good" matrices and low score "bad". Now that you've got normally distributed values you can pick one of many stats to analyze them. But even if they're not normal there are still a variety of options either non-parametric or one that models the observed distribution.
