Magnesium and calcium are both harder than sodium and potassium. This is because magnesium and calcium have higher melting and boiling points compared to sodium and potassium, which indicates that they have stronger metallic bonds and are therefore harder. Additionally, magnesium and calcium have higher densities than sodium and potassium, which also contributes to their hardness.