Interacting with lists is one thing that virtually each coder does day by day. Sorting lists are among the many most common duties we consider lists. When it issues primitive sorts and strings, sorting lists in C# is somewhat easy, however it turns into considerably tougher after we make lists of our objects. We are going to go over a number of the commonest C# features for sorting lists on this article. Sorting is an strategy for arranging parts in an ordered sequence from an inventory of unsorted gadgets. We ceaselessly add gadgets to a Checklist in C# packages after which have to type them afterward. For this, we use the Kind operate. Different sorting strategies can be found too for superior sorting.

What are the listing type in C# in Ubuntu 20.04:

The Checklist.Kind() technique compares listing gadgets utilizing both the given or default IComparer<T> implementation or a given Comparability<T> delegate to type the weather or a subset of the gadgets within the Checklist<T>. The Kind() technique is a built-in operate in C# that gives in-place sorting on an inventory of objects. In consequence, the type() operate is used to prepare an inventory in ascending or descending order. One other factor to remember is that the type() operate completely rearranges the listing.

Tips on how to use listing type in C# in Ubuntu 20.04:

The sorting listing can be utilized in a number of other ways. The Kind, Reverse, ThenBy, and OrderBy strategies could also be used to type the listing in ascending and descending order, respectively. To higher perceive how the listing is sorted within the C# programming language, think about the next examples.

Instance # 1: Utilizing Checklist Kind Methodology for Ascending order in C# in Ubuntu 20.04:

Sorting an inventory of fundamental sorts equivalent to int, double, char, string, and other forms in C# is sort of easy. All now we have to do right here is use the Generic Checklist class’s Kind() operate on the listing occasion, and the info might be dynamically sorted in ascending order.

Earlier than beginning the implementation of the above program, now we have included the required libraries “System” and “System.Collections.Generic” of C# with the key phrase “utilizing”. The “System.Collections.Generic” permits customers to construct strongly typed collections with improved kind security and pace. Then, now we have outlined a category “sort_example” and invoked its principal operate. In the principle operate, now we have an inventory of integers as “sortList”. We now have added the random numbers within the integers listing with the assistance of the Add() technique. The WriteLine assertion will print the listing of integers.

We now have used a foreach loop for the integer listing that can iterate over every integer worth within the listing. The “in” key phrase within the foreach loop is used to pick out gadgets from the listing on every iteration and assign it to the variable “worth” of the int kind created within the foreach loop. After this, now we have a kind() technique for the given integer listing, which types the listing in ascending order. The kind() technique, by default, organizes the listing into ascending order. The foreach loop is once more used for the iteration over every worth within the sorted listing. The writeLine technique will show the sorted listing in ascending order.

The output shows the integer listing earlier than sorting and after sorting within the following picture.

Instance # 2: Utilizing Reverse Methodology for Descending Order in C# in Ubuntu 20.04:

The reverse() technique is a helpful strategy for sorting a Checklist<T> in reducing order. The reverse() operate is used to reorder all the values in an inventory. Within the instance program under, First, we used the type() operate to prepare the listing in ascending order. After that, type the listing in reducing order with the reverse() operate.

We now have included the libraries which can be wanted in this system. After that, the category is constructed with the identify “SortDescendingOrder”. The category has the principle operate the place now we have declared an inventory of integers as “SortDesc”. The “SortDesc” incorporates the listing of integers and provides the random integer values within the listing via the tactic referred to as Add(). The foreach loop is used after the listing initialization. The foreach loop iterates over every listing merchandise and assigns the listing values to a variable “num”.

Then, now we have a kind() technique, which by default organizes the listing into ascending order. Now, we’re utilizing the Reverse() technique, which types the integers listing in descending order. The foreach loop iterates over every descending order listing. The sorted listing and the sorted listing in descending order might be printed by the WriteLine assertion.

The output clearly exhibits the working of the type technique and reverse technique within the following picture.

Instance # 3: Utilizing OrderBy Methodology for sorting listing in C# in Ubuntu 20.04:

That is another approach to type an inventory in ascending order. We simply have to make use of the Methodology OrderBy() to name the listing identify outlined within the code block after which cross an argument in opposition to which the gadgets might be verified to type it in ascending order.

The above program has a category with the general public key phrase and is outlined as “Colours”. We now have declared the 2 variables “SNo” of kind integer, and “Title” is about to string kind property. Then, we created a constructor of the category “Colours” and handed the category members as a parameter. Within the physique of the constructor, now we have used the “this” key phrase with the category members. The key phrase “this” will relate to this system’s class occasion members.

After that, the override ToString technique is applied, which returns the string illustration of the given class members. We now have created one other class, “Demo” and referred to as the principle operate of the category. In the principle operate, now we have created three objects, C1, C2, and C3, for the category “Colours” and assigned values to them. Then we outlined an inventory for the category “Colours” and assigned the category objects to it. The brand new listing is created as “sortList” which holds the “colList” with the orderBy technique. Right here now we have handed “SNo” into the OrderBy technique, which types the listing by “SNo”. The WriteLine assertion will print the OrderBy listing, and the listing might be cut up into new traces as now we have “Surroundings.NewLine” within the string be a part of technique.

The output of the OrderBy listing is proven because the cut up string within the following display screen.

Instance # 4: Utilizing ThenBy and ThenByDescending Methodology for sorting Checklist in C# in Ubuntu 20.04:

Sorting on a number of fields is achieved utilizing the ThenBy and ThenByDescending extension strategies. After the OrderBy technique, name the ThenBy technique to type the gathering in ascending order by one other discipline. Equally, implement extra sorting in descending order utilizing the ThenByDescending operate.

The above program has a category outlined as “ThenBymethod”. Within the class principal operate, now we have created an inventory of sophistication “Pupil” which is applied on the finish. The listing units the worth of the fields “ID” and “Title” of the category “Pupil”. Then, now we have declared a variable “Consequence” which types the listing by the tactic OrderBy after which sorted by the ThenBy technique of the desired fields.

In the identical method, now we have used the ThenByDescending technique for lists to be sorted in descending order in a variable “ResultDesc”. The foreach loop will iterate over every worth within the sorted listing of each strategies. Within the final class, “Pupil” through which now we have a get and set the property for the sector “ID” and “Title”.

The next outcomes are obtained from the ThenBy and ThenByDescending strategies.


We now have concluded the article of C# listing type right here. Within the article, now we have mentioned the usage of the listing type technique. We now have a concise introduction to the C# listing string after which have a exact definition of the listing type technique. We now have defined type the listing in C# with completely different strategies. We now have implementations of the instance which can be sorted in 4 other ways, such because the thorough type OrderBy and ThenBy technique for ascending order. The Reverse and ThenBydescending strategies are used for descending order.

Did you want this text?
Share it on any of the next social media channels under to offer us your vote. Your suggestions helps us enhance.