modified WordSort since the complexity was too high