Create sorting function for words array