Pavi S

Pavi S

  • NA
  • 41
  • 74.4k

Bubble sort using string array in C#

Jan 26 2012 11:32 PM
please help  me to sort the string array in bubble sort method.

Please help me sort [without using Collectons or Array.Sort()] the string array in the following code snippet:


using System;

class MainClass
{
    static void Main()
    {  
        string[] s = {"Bill", "Gates", "James", "Apple", "Net", "Java"};

        try
        {
            for(int i=0; i<s.Length; i++)
            {
                for(int j=0; j<s.Length-1; j++)
                {
                    for(int k=0; k<s.Length; k++)            
                        if(s[j][k] > s[j+1][k])
                        {
                            string temp = s[j];
                            s[j] = s[j+1];
                            s[j+1] = temp;
                        }
               
                }
            }             
        }

        catch(Exception)
        {

        } 

        for(int i=0; i<s.Length; i++ )
        {
            Console.WriteLine(sIdea + " ");
        }               
    }
}




the above code throwing error please help me to sort string array.


Answers (3)