How To Sort Array In C#

C# sort array or sort C# array. Sorting a C# array is a common operation for programmers. In this article and code examples, you will learn how to sort an array in C# in ascending order. You will also see how to sort an array in C# in descending order using the Reverse method. You will also learn how to sort a partial array or a range of array elements.

The simplest way to sort an array in C# is using Array.Sort method. The Array.Sort method takes a one-dimensional array as an input and sorts the array elements in the ascending order.

The following code snippet creates an array of integers.

int[] intArray = new int[] { 9, 2, 4, 3, 1, 5 };

The Array.Sort method takes array as an input and sorts the array in ascending order.

Array.Sort(intArray);

To sort an array in descending order, we can use Sort.Reverse method. This method also takes an array as an input and sorts its elements in descending order. The following code snippet reverses an array.

Array.Reverse(intArray);

Sort an Array of Int in C#

Let’s learn how to sort an array of int in C#.

Here is the complete code that creates an array of integers and sorts in ascending and descending orders using Array.Sort and Array.Reverse methods.

static void Main(string[] args) {
        // Array of integers
        int[] intArray = new int[] {
            9,
            2,
            4,
            3,
            1,
            5
        };
        Console.WriteLine("Original array");
        foreach(int i in intArray) {
            Console.Write(i + " ");
        }
        Console.WriteLine();
        // Sort array in ASC order
        Console.WriteLine("Sorted array in ASC order");
        Array.Sort(intArray);
        foreach(int i in intArray) {
            Console.Write(i + " ");
        }
        Console.WriteLine();
        Console.WriteLine("Sorted array in DESC order");
        // Sort Array in DESC order
        Array.Reverse(intArray);
        foreach(int i in intArray) {
            Console.Write(i + " ");
        }
        Console.WriteLine();

The output of the above code is below where first it prints the original array, followed by the sorted array in ASC and DESC order respectively.

How To Sort An Array In Csharp

Sort an Array of Strings in C#

Now let’s sort an array of strings in C#. Sorting an array of strings is like sorting an array of int. The Array.Sort method takes an input value of an array of strings. The following code example shows how to sort an array of strings in ascending and descending orders using C#.

// Array of strings
string[] strArray = new string[] { "Mahesh", "David", "Allen", "Joe", "Monica" };
Console.WriteLine();
Console.WriteLine("Original array");
foreach (string str in strArray)
{
    Console.Write(str + " ");
}
Console.WriteLine();
// Sort array 
Array.Sort(strArray);
// Read array items using foreach loop
foreach (string str in strArray)
{
    Console.Write(str + " ");
}
Console.WriteLine();
Array.Reverse(strArray);
foreach (string str in strArray)
{
    Console.Write(str + " ");
}
Console.WriteLine();

The output of the above code displays original array, sorted array in the ascending order, and sorted array in the descending order.

How To Sort An Array In Csharp

Sort a Range of Elements In An Array

The Sort.Array method allows you to sort a range of elements within an array. For example, if an array has 11 elements but what if you want to sort only 1st to next 6 elements in the array? You can use that by passing the first starting index of the element followed by the number of elements to be sorted.

public static void Sort (Array array, int index, int length);

The following code snippet sorts 6 elements in array starting from the 1st element in the array.

// Array of integers
int[] intArray = new int[] { 9, 2, 4, 3, 1, 5, 6, 9, 5, 7, 1, 0};
// Sort array from 1st element to 6th element. Skil 0th element.
Array.Sort(intArray, 1, 6);
foreach (int i in intArray)
{
    Console.Write(i + " ");
}

The output looks like the following where you can see only 6 elements are sorted after skipping the first element of the array.

9 1 2 3 4 5 6 9 5 7 1 0

Note that this sorting applies to all types of arrays not just integers.

Summary

In this code example, we learned how to use the Array.Sort method to sort a one-dimensional array of integers and strings in C#.

Here is a detailed tutorial on Arrays in C#.


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.