c sharp

C# Sort List

The method of arranging data in alphabetical order or in ascending/ descending order. This method is known as the sorting of data. The sorting method makes it easy for us to find the information quickly, which we want. In C# programming, a sorted list means that the elements of the list are arranged in a specific order. Most commonly the alphabets and words are stored in alphabetical order and numbers are stored in ascending or descending order. The sort list is one in which the elements of data are sorted in a specific order. In this article, we will study in detail, the sorting of list elements in C# programming in Ubuntu 20.04.

Methods of Sorting List in C# Programming:

Here, we discuss different methods to sort lists in C# programming.

  • Sort()
  • Sort(IComparer<T>)
  • Sort(Int32,Int32,IComparer)
  • Sort(Comparison<T>)

Example # 1: Sorting the List of Integers by Using the Sort() Method

In this example, we are using the Sort() method to sort the list in C# programming in Ubuntu 20.04. Now, run this given code on your text editor and you will know how this method will sort the list elements. We are going to run this code in the text editor of Ubuntu 20.04. The code is given below in the image.

In this example, we import the C# library “using System” for getting the methods and also functions of the C# program. The “using System.Collections.Generic” is imported here that contains interfaces and classes. Also, we import “using System.Linq” to retrieve data. After all this, we are using the namespace “Sorting”. Inside this namespace, we declare a class named “Program” which is public. Now, in this class, we invoked a “main” function which is the main part of the C# program.

After this, we create a list of integers called “numbers”. The “new” keyword is used to create a new list of integers and also, we initialize this list with a list of numbers. Lets’ print these numbers using “Console.WriteLine”. Before printing this unsorted list, first, we print a line “unsorted list in C# program” using “Console.WriteLine”. And then we use the “foreach” loop. This “foreach(int number in numbers)” will get the number in the numbers list. Now we want to display these numbers using “Console.WriteLine”. Now, it’s time to sort our list and for this, we are using the “Sort()” method.

The “numbers.Sort()” will sort these numbers in ascending order. After this sorting, we are going to print these sorted numbers on the screen. Before displaying sorted numbers, we print a line “Sorted List in Ascending Order in C# Program” by using “Console.WriteLine” and then use the “foreach” loop. This “foreach” loop now gets the sorted numbers and displays these numbers using “Console.WriteLine”. We also sort this list in descending order. For this, we are using “numbers.Reverse()”. This method will reverse the sorted list in descending order. Now, we again print a line before displaying the numbers in descending order using “Console.WriteLine”. Then, again use the “foreach” loop and “Console.WriteLine” to print the list numbers in reverse or descending order.

For the output, we use the commands which are shown in the given image. The “mcs” command is used for the compilation of the program having an extension of “.cs” with the filename. The “mono” command is used for the execution of a program having an extension of “.exe” with the filename.

In this output, we see that first, it prints the unsorted list numbers. After this, it prints the sorted list numbers in ascending order, and at the last, it prints the list numbers in reverse or descending order. This is the sorting of list numbers in ascending and descending order.

Example # 2: Sorting the List of Strings by Using the Sort() Method

In this example, we are going to sort the string list in alphabetical order using the “Sort()” method and print the unsorted and sorted string list on the terminal.

This code is the same as we have discussed in the previous example. In the first line of code, we use “using System” to get the functions and methods and then “using System.Collections.Generic” namespaces for interfaces and classes and also “using System.Linq” for retrieving data which we have discussed in the previous code. After this, we created a namespace named “Sorting” and a public class with the name “Program”.

Now, we invoked a “main” method as “public static void Main()”. Inside this “main” function, we have a list of “strings” named “words” and pass some string data which we want to sort to this string list. We display the line “Unsorted names” using “Console.WriteLine” and then for printing these unsorted names we are using the “foreach” loop. This loop will access the words in a string and “Console.WriteLine” prints these unsorted names on the display screen.

We are going to sort the string list in alphabetical order and for this purpose, we use the “Sort()” method. The “words.Sort()” will sort the string list data in alphabetical order and then gets these sorted data using the “foreach” loop. After this, for printing these alphabetically sorted data we are using “Console.WriteLine”.

We will get the output with the same method, using the same commands as we have discussed in the above code but the filename is different. The output of this code is here for you to better understand this concept.

The output is shown in this image. We see that it prints all the string list data unsorted the first time and then sorts these given names and also prints the sorted names in alphabetical order on the output screen.

Example # 3: Sort the List by Using the Sort(IComparer<T>)

Now, look at this example in which the “Sort(IComparer<T>)” method works in the C# program in Ubuntu 20.04. The code is here in the image given below.

In the code given above, we are using the “using System” library, “using System.Collections.Generic” and the “using System.Linq”. We have explained all these namespaces in our previous examples. After this, we create a class with the name “CSharp” which is inherited from “IComparer<int>”. After this, we have “public int Compare(int a, int b)” in which we declare two integers “a” and “b” with the integer data type. Now, we have “if” statement in which we have a condition “a==0 || b==0”. If this condition is satisfied, then return 0 and if the condition is not satisfied then compare a and b and return the result.

For this, we are using “return a.CompareTo(b)”. Now, we have a public class named “Program” and the “main” method. After invoking this “main” method, we create a “new” list of integers named “list1” and pass integer numbers to that list. Then, in the next line, we print “List Before Sorting” using the “Console.WriteLine”. The “foreach” loop is used to get the integer numbers in “list1” and “Console.Writeline” will render these unsorted integers on the screen, as we discussed in the previous example.

Now in the next line of code, we create an object “srt” of class “CSharp”. After creating an object, we render a line on the output by using “Console.WriteLine”. And then going to sort list1 using “list1.Sort(srt)”. This will sort the integers in ascending order. And again, we use “foreach” loop and “Console.WriteLine” which we discussed many times for displaying the sorted list. The output of this code is here.


Here, we have explained how to sort a list in C# programming in Ubuntu 20.04. We discussed two methods here in this article. We demonstrate different examples for better understating this concept of sorting lists in C# programming. Sorting a list is somehow similar to sorting Array in C# programming. I hope this article will help you a lot in understanding this concept and you will be able to sort the list in the C# program after reading this article. We have explained all the elements of the C# program in detail and also provide code and output for you so that you can easily learn how to sort the list and be able to use them in complex programs of C#.

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.