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.
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