lundi 17 avril 2023

How do i sort the words in a vector in alphabetical order?, (all letters in each word need to be accounted for)

Im trying to make a function that sorts the words in a vector an puts them in alphabetical order, and if the letters are all the same letters the word with fewest letters ends up higher in the vector.

Right now everything seems to work except when words with all the same letters end up in a random order.

For examlpe the words a, aaa and aaaa would end up in a random order but i would want a to be at the top of the list and aaaa at the bottom.

void sortWords()
{
    for (int i = 0; i < wordList.size(); i++)
    {
        for (int j = 0; j < wordList.size(); j++)
        {
            string temp;
            for (int y = 0; y < wordList[i].size(); y++)
            {
                if (wordList[i][y] < wordList[j][y] && i != j) 
                {
                    temp = wordList[i];
                    wordList[i] = wordList[j];
                    wordList[j] = temp;
                    break;
                }
                else if (wordList[i][y] > wordList[j][y] && i != j)
                {
                    break;
                }

            }
                
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire