Assortative mechanisms can overcome tragedies of the commons that otherwise result in dilemma situations. Assortativity criteria include various forms of kin selection, greenbeard genes, and reciprocal behaviors, usually presuming an exogenously fixed matching mechanism. Here, we endogenize the matching process with the aim of investigating how assortativity itself, jointly with cooperation, is driven by evolution. Our main finding is that full-or-null assortativities turn out to be long-run stable in most cases, independent of the relative speeds of both processes. The exact incentive structure of the underlying social dilemma matters crucially. The resulting social loss is evaluated for general classes of dilemma games, thus quantifying to what extent the tragedy of the commons may be endogenously overcome.