c sharp

C# List Sort

Interacting with lists is something that practically every coder does daily. Sorting lists are among the most typical tasks we think of lists. When it concerns primitive types and strings, sorting lists in C# is rather simple, but it becomes somewhat more challenging when we make lists of our objects. We will go over some of the most common C# functions for sorting lists in this article. Sorting is an approach for arranging elements in an ordered sequence from a list of unsorted items. We frequently add items to a List in C# programs and then need to sort them afterward. For this, we use the Sort function. Other sorting methods are available too for advanced sorting.

What are the list sort in C# in Ubuntu 20.04:

The List.Sort() method compares list items using either the given or default IComparer<T> implementation or a given Comparison<T> delegate to sort the elements or a subset of the items in the List<T>. The Sort() method is a built-in function in C# that provides in-place sorting on a list of objects. As a result, the sort() function is used to organize a list in ascending or descending order. Another thing to keep in mind is that the sort() function permanently rearranges the list.

How to use list sort in C# in Ubuntu 20.04:

The sorting list can be used in several different ways. The Sort, Reverse, ThenBy, and OrderBy methods may be used to sort the list in ascending and descending order, respectively. To better understand how the list is sorted in the C# programming language, consider the following examples.

Example # 1: Using List Sort Method for Ascending order in C# in Ubuntu 20.04:

Sorting a list of basic types such as int, double, char, string, and other kinds in C# is quite simple. All we have to do here is use the Generic List class’s Sort() function on the list instance, and the data will be dynamically sorted in ascending order.

Before starting the implementation of the above program, we have included the required libraries “System” and “System.Collections.Generic” of C# with the keyword “using”. The “System.Collections.Generic” enables users to build strongly typed collections with improved type safety and speed. Then, we have defined a class “sort_example” and invoked its main function. In the main function, we have a list of integers as “sortList”. We have added the random numbers in the integers list with the help of the Add() method. The WriteLine statement will print the list of integers.

We have used a foreach loop for the integer list that will iterate over each integer value in the list. The “in” keyword in the foreach loop is used to select items from the list on each iteration and assign it to the variable “value” of the int type created in the foreach loop. After this, we have a sort() method for the given integer list, which sorts the list in ascending order. The sort() method, by default, organizes the list into ascending order. The foreach loop is again used for the iteration over each value in the sorted list. The writeLine method will display the sorted list in ascending order.

The output displays the integer list before sorting and after sorting in the following image.

Example # 2: Using Reverse Method for Descending Order in C# in Ubuntu 20.04:

The reverse() method is a useful approach for sorting a List<T> in decreasing order. The reverse() function is used to reorder all of the values in a list. In the example program below, First, we used the sort() function to organize the list in ascending order. After that, sort the list in decreasing order with the reverse() function.

We have included the libraries that are needed in the program. After that, the class is built with the name “SortDescendingOrder”. The class has the main function where we have declared a list of integers as “SortDesc”. The “SortDesc” contains the list of integers and adds the random integer values in the list through the method called Add(). The foreach loop is used after the list initialization. The foreach loop iterates over each list item and assigns the list values to a variable “num”.

Then, we have a sort() method, which by default organizes the list into ascending order. Now, we are using the Reverse() method, which sorts the integers list in descending order. The foreach loop iterates over each descending order list. The sorted list and the sorted list in descending order will be printed by the WriteLine statement.

The output clearly shows the working of the sort method and reverse method in the following image.

Example # 3: Using OrderBy Method for sorting list in C# in Ubuntu 20.04:

This is an alternative way to sort a list in ascending order. We just have to use the Method OrderBy() to call the list name defined in the code block and then pass an argument against which the items will be verified to sort it in ascending order.

The above program has a class with the public keyword and is defined as “Colors”. We have declared the two variables “SNo” of type integer, and “Name” is set to string type property. Then, we created a constructor of the class “Colors” and passed the class members as a parameter. In the body of the constructor, we have used the “this” keyword with the class members. The keyword “this” will relate to the program’s class instance members.

After that, the override ToString method is implemented, which returns the string representation of the given class members. We have created another class, “Demo” and called the main function of the class. In the main function, we have created three objects, C1, C2, and C3, for the class “Colors” and assigned values to them. Then we defined a list for the class “Colors” and assigned the class objects to it. The new list is created as “sortList” which holds the “colList” with the orderBy method. Here we have passed “SNo” into the OrderBy method, which sorts the list by “SNo”. The WriteLine statement will print the OrderBy list, and the list will be split into new lines as we have “Environment.NewLine” in the string join method.

The output of the OrderBy list is shown as the split string in the following screen.

Example # 4: Using ThenBy and ThenByDescending Method for sorting List in C# in Ubuntu 20.04:

Sorting on multiple fields is accomplished using the ThenBy and ThenByDescending extension methods. After the OrderBy method, call the ThenBy method to sort the collection in ascending order by another field. Similarly, implement additional sorting in descending order using the ThenByDescending function.

The above program has a class defined as “ThenBymethod”. In the class main function, we have created a list of class “Student” which is implemented at the end. The list sets the value of the fields “ID” and “Name” of the class “Student”. Then, we have declared a variable “Result” which sorts the list by the method OrderBy and then sorted by the ThenBy method of the specified fields.

In the same way, we have used the ThenByDescending method for lists to be sorted in descending order in a variable “ResultDesc”. The foreach loop will iterate over each value in the sorted list of both methods. In the last class, “Student” in which we have a get and set the property for the field “ID” and “Name”.

The following results are obtained from the ThenBy and ThenByDescending methods.

Conclusion:

We have concluded the article of C# list sort here. In the article, we have discussed the use of the list sort method. We have a concise introduction to the C# list string and then have a precise definition of the list sort method. We have explained how to sort the list in C# with different methods. We have implementations of the example that are sorted in four different ways, such as the thorough sort OrderBy and ThenBy method for ascending order. The Reverse and ThenBydescending methods are used for descending order.

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.