This is a complete misconception. Swordtails do not undergo sex changes. The only reason people think this is because certain fish take longer to develop than others. Your fish was always male. It just developed its gonopodium and sword extension late. Also, if it's true that this development is most common in situations where there are no other males, the explanation would be that the fish don't want to be in a position where they'll attract negative attention from a dominant male so, as is the case with many dimorphic cichlid species, the young, subordinate, fish have their male characteristics suppressed to avoid conflict.