Political polarization has become a growing concern in democratic societies, as it drives tribal alignments and erodes civic deliberation among citizens. Given its prevalence across different countries, previous research has sought to understand under which conditions people tend to endorse extreme opinions. However, in polarized contexts, citizens not only adopt more extreme views but also become correlated across issues that are, a priori, seemingly unrelated. This phenomenon, known as "ideological sorting", has been receiving greater attention in recent years but the micro-level mechanisms underlying its emergence remain poorly understood. Here, we study the conditions under which a social dynamic system is expected to become ideologically sorted as a function of the mechanisms of interaction between its individuals. To this end, we developed and analyzed a multidimensional agent-based model that incorporates two mechanisms: homophily (where people tend to interact with those holding similar opinions) and pairwise-coherence favoritism (where people tend to interact with ingroups holding politically coherent opinions). We numerically integrated the model's master equations that perfectly describe the system's dynamics and found that ideological sorting only emerges in models that include pairwise-coherence favoritism. We then compared the model's outcomes with empirical data from 24,035 opinions across 67 topics and found that pairwise-coherence favoritism is significantly present in datasets that measure political attitudes but absent across topics not considered related to politics. Overall, this work combines theoretical approaches from system dynamics with model-based analyses of empirical data to uncover a potential mechanism underlying the pervasiveness of ideological sorting.