
-----------------------------------
whoareyou
Wed Apr 20, 2011 7:31 pm

help - listing strings in alphbetical order
-----------------------------------
So basically, this program that I'm supposed to be making has a part where I need to be sort these 2 entered strings alphabetically. So far, what I've done is to create a for loop that will check the first letter of the two words and will determine the order according to the first letter. But then I realized that there were problems, because what if both words started with the same letter, then I'd have to check the next letter, and if that letter was the same, I'd have to check the next letter. If the two strings were equal, I could check that at the beginning by using a simple if statement. So I think, fundamentally, my question is how would I go about checking every letter of the words, and determining whichever one comes first? Oh, and also, if all the letters were the same but one of the strings were longer, then the first string would be first, then the second.

[code]
char lword1 = word1.charAt (0);
        char lword2 = word2.charAt (0);

        for (int i = 65 ; i = 91 && i  0)
        {
            c.print (word2 + ", ");
            c.println (word1);
        }

        else if (b < 0)
        {
            c.print (word1 + ", ");
            c.println (word2);
        }


        else if (b == 0)
        {
            c.print (word1 + ", ");
            c.print (word2);
            c.println (" [THE WORDS ARE THE SAME!]");
        }
[/code]

I think this is error proof.
