Please Help me to bubble sort of a String array.

for (int pass = 1; pass <= wordsArray.Length; pass++)
    {
        for (int j = 0; j <= wordsArray.Length - 2; j++)
        {
           [B] if (wordsArray[j] > wordsArray[j + 1])[/B]    i find the error over this line.. .logical error i think please help me      
                {
                string Swap; 
                Swap = wordsArray[j];
                wordsArray[j] = wordsArray[j + 1];
                wordsArray[j + 1] = Swap;
            }
        }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

The string class has no overloaded > operator. Use a Compare method of the string class for that.
EDIT: You can only use == and != operators in an if statement when comparing strings.

Edited 5 Years Ago by ddanbe: n/a

Hi

string[] wordsArray = { "a", "bb", "ccc", "d", "ee", "f", "ggg" };
            for (int pass = 0; pass < wordsArray.Length; pass++)
            {
                for (int j = 0; j <= wordsArray.Length - 2; j++)
                {
                    if (wordsArray[j] > wordsArray[j + 1]) //when you come the last character of the array index [j+1] will throw an error - becuase its out of bounds!!
                    {
                        string Swap;
                        Swap = wordsArray[j];
                        wordsArray[j] = wordsArray[j + 1];
                        wordsArray[j + 1] = Swap;
                    }
                }
            }

What exactly would you like to do here?

Why you want to do that? Just use the Sort method of the array.

One word: Homework

Comments
Guess I was in outer space, thanks for bringing me back. :)
This article has been dead for over six months. Start a new discussion instead.