c sharp

C# Sort Array

The process of organizing data in a specific order to make it easier to find information is known as sorting. A sorted array is where every item is kept at evenly spaced locations in memory storage and is structured in numerical, alphabetical, or other order. Arranging the elements of an array in a specific order is called array sorting. This article will discuss how to arrange an array in a particular order. We will sort arrays in C# by using different methods. Here, we will deliberate some instances.

 Example 1: Sort array using Array.Sort and Array.Reverse method

In C# “Array.Sort method” is the modest approach to sort an array. In the example given below, we sorted an array with the help of the “Array.Sort method”.

In the above code, we have the keyword “using”. We can include namespaces by “using” keyword. Now, we add the “using System.Collections.Generic” namespace which contains interfaces and classes for generic collections, allowing users to create strongly typed collections that are way more efficient.

After that, we have a class. The class is named “Sorting”. We had “static public void Main()”, after this class in brackets. Just below the main function, we declare and initialize an “array of integers”e enter “6 integers” in an array in unsorted form. Then, we enter those numbers in an array that we want to sort in a specific order. Now, for printing the message on the screen, we use “Console.WriteLine()”. Anything which is written inside this will print on the screen.

After this, with the help of the “foreach” loop, we access all the array elements. The “foreach(int a in intArray)” will get all the integers from the array. Now, we use “Console.WriteLine(a)” to display the integers of an array on the screen. Before sorting an array, we display the message “Sorted Array in Ascending Order” by using “Console.WriteLine”. For sorting arrays, we use the “Array.Sort(intArray)” method. After this, the “foreach” loop is used. The “foreach(int a in intArray)” will access array integers in ascending order. Now, we print this sorted array with the help of “Console.WriteLine(a)”.

After sorting the array in ascending order, we move to our next step to reverse this array. Before reversing the array, we print a line “Sorting Array in Descending Order” by using “Console.WriteLine”. For reversing the array, we use the “Array.Reverse method”. The “Array.Reverse(intArray)” reverses the array. After this, the“foreach” loop helps to get integers from the array, and “Console.WriteLine” prints all integers in descending order.

After completing the above code, save this file with any name and extension of .cs.

Now for getting the output, the following commands are well-used:

$ mcs arraySort.

This command is used to compile the code and create an “.exe” file. In this command, the Mono compiler is “mcs”. Here “arraySort” is the name of the file which we save with extension .cs.

$ mono arraySort.cs

This command is used for executing the C# program. It runs the program which we compiled in the above command. It executes the arraySort.exe file and displays the output for our C# program.

Example 2: Sorting of String Array

In this example, we see that we can sort an array of a string the same as we sort an array of integers with the help of the “Array.Sort method”.

In this code, to add namespaces “using” keyword is used. After that “using System.Collections.Generic” is used which is explained in the above example. This code is the same as we have in the first example but in this code, we are sorting an array of strings in alphabetical order. We defined a class as the “string” class. After this, we have the “main()” function. Inside the main() function, we declare and initialize an “array of type string”. We pass some strings data to this array that is “David”, “Micheal”, “Arvid”, “James”, and “Henry”.  Now we display a line on the screen “String Array before Sorting” by using “Console.WriteLine”. After displaying this line, we show all the string array names on the screen and for this, we use the “foreach” loop.

The “foreach(string s in arrayStr)” is used for getting all the strings from the array and “Console.WriteLine(s)” prints them on the screen. Now we are going to sort our array of strings in alphabetical order. Before this, we print a line “Sorted Array in Alphabetical Order” by using the same “Console.WriteLine” function. Then, we sort string arrays by using the “ArraySort method”. After this, we print all these sorted arrays using the “foreach” loop as we described above.

We get the output by using commands as we describe in the overhead illustration. In this output, we see that the array is sorted in alphabetical order.

Example 3: By using an iterative way

In this example, we don’t use any built-in method. Instead of any built-in function, we are using an iterative way to sort our array.

In this code, first, we use “using System” for adding namespaces to our program.  After that, we add the “using System.Collections.Generic” namespace as described in the above example. Below this namespace, we make the class of name “iterative”. Inside this class, we define our main() function. After the main() function, we declare and initialize the array and assign “9,5, 6,8,1” values to this array. Then, we define another variable named “temp” as an integer which we use later in this code.

After that, we have two “for loops”. In the first “for loop,” we define an integer named “s” and assign “0” to “s”. Then, we have a condition “s < num.Length – 1” after checking this condition, if the condition is true it moves to the second loop and repeat the same process until the condition is false. In the second loop, we initialize another integer “t”.  In this, traverse “s+1” to “num.Length”. If the condition of the second loop is true it moves to the “if statement”. Now, if num[s] less than num[t] “if(num[s] < num[t])” then the statements below in curly braces will execute. If the condition is true then we assign the “num[s]” value to “temp” and the “num[t]” value to “num[s]” and “temp” to “num[t]”. In short, we swap all the values. All values are swapped until the array is sorted in descending order.

In both loops the value of “s” and “t” increments each time the loop executes. After this, we have a “foreach” loop. In this “foreach” loop “foreach(int value in num)” int describes the integer value in an array named “num”. After this, we have “Console.WriteLine” to print values.

For getting the output, the same commands will run as we explained in the previous example. Before execution, we compile this code using the command given below and then execute that program. And get the output.


In this guide, we have explored how to sort arrays in C# in detail. We concentrated on Arrays because they cover all of the primitive kinds. We use a built-in function and also sort an array using the iterative method. Once you have mastered the concept of sorting in C#, it will be simple to apply it to other collections like enumerations, dictionaries, and so on.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.