Sponsored Ad

Monday, January 17, 2011

Sort an Array using Inbuilt Array Functions in C#

Do you have a array and would like to sort the array. C# provide a inbuilt function to sort the array. You need to call Array.Sort() function and pass the array as an parameter to it. This calling is by reference and the sort function will change the original array. once you print the array it will be in sorted order.

Sort an Array using Inbuilt Array Functions in C#

using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strArray = { "one", "two", "three", "four" };
            Console.WriteLine("Before Sorting: ");
            foreach(string str in strArray)
                Console.Write(str + " ");
            Array.Sort(strArray);

            Console.WriteLine("");
            Console.WriteLine("After Sorting: ");
            foreach (string str in strArray)
                Console.Write(str + " ");

            Console.ReadLine();
          }         
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates