How to Sort An Array in C#/CSharp

For sorting an array in C#/CSharp, we make use of static method called sort which resides in Array class.  Example is given below.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
int[] array = { 9, 8, 7, 4, 3 };
Console.WriteLine("Before Sorting");
foreach (int i in array)
{
Console.WriteLine(i);
}
Console.WriteLine("---------------------------");
Array.Sort(array);
Console.WriteLine("After Sorting");
foreach (int i in array)
{
Console.WriteLine(i);
}
}
}
}
using System; namespace Hello_World { class Program { static void Main(string[] args) { int[] array = { 9, 8, 7, 4, 3 }; Console.WriteLine("Before Sorting"); foreach (int i in array) { Console.WriteLine(i); } Console.WriteLine("---------------------------"); Array.Sort(array); Console.WriteLine("After Sorting"); foreach (int i in array) { Console.WriteLine(i); } } } }
using System;

namespace Hello_World
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 9, 8, 7, 4, 3 };

            Console.WriteLine("Before Sorting");
            foreach (int i in array)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine("---------------------------");

            Array.Sort(array);

            Console.WriteLine("After Sorting");
            foreach (int i in array)
            {
                Console.WriteLine(i);
            }
        }               
    }
}