The reason is that the positive part of one compound must combine with the negative part of the other compound.
This is an example of a double replacement reaction.
The more positive part of a compound comes first in the formula.
We could write the reaction between AB +CD → ? as
A⁺B⁻ + C⁺D⁻ → ?
When the positive ions change partners, A⁺ combines with D⁻, and C⁺ combines with B⁻.
The reaction is then
A⁺B⁻ + C⁺D⁻ → A⁺D⁻ + C⁺B⁻ or AB + CD → AD + CB
The formula BC would say incorrectly that B is the positive part of the compound.
Thus, in your reaction,
2Na⁺,2Cl⁻(aq) + Hg₂²⁺,2NO₃⁻ (aq) → 2Na⁺, 2NO₃⁻(aq) + Hg₂²⁺,2Cl⁻(s)