Sort elements in a String Array – C Sharp / C#

By | June 21, 2011

Hi,

In order to sort the elements in a string array in C Sharp/C#, we use the sort() method. Given below is a simple example using sort() method to sort the elements in a string array.

using System;

class MainClass
{

  public static void Main()
  {

    string[] urStringArray = {"xylo", "apple", "zeebra", "coderz", "heaven123", "heaven777"};
    Array.Sort(urStringArray );
    Console.WriteLine("Sorted string Array:");
    for (int i = 0; i < urStringArray.Length; i++)
    {
      Console.WriteLine("urStringArray[" + i + "] = " + urStringArray[i]);
    }
  }

}

It will print out,
Sorted string Array:
urStringArray[0] = apple
urStringArray[1] = coderz
urStringArray[2] = heaven123
urStringArray[3] = heaven777
urStringArray[4] = xylo
urStringArray[5] = zeebra

🙂

Leave a Reply

Your email address will not be published. Required fields are marked *