C# Sort Array of Integers

To sort an array of integers in increasing order in C#, call Array.Sort() method and pass the array as argument. The elements in the array are sorted in ascending order.

The order of the elements in the original array will be modified.

Example

In the following example, we take an array of integers arr, and sort this array in ascending order using Array.Sort().

Program.cs

</>
Copy
using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            int[] arr = { 3, 5, 12, 6, 1 };
            Array.Sort( arr );
            for (int i = 0; i < arr.Length; i++) {
                Console.WriteLine( "arr[{0}]: {1}", i, arr[i] );
            }
        }
    }
}

Output

arr[0]: 1
arr[1]: 3
arr[2]: 5
arr[3]: 6
arr[4]: 12

Conclusion

In this C# Tutorial, we learned how to sort an array of integers, with examples.