This is a common scenario that we face while working. We want to sort the list based on the different value of its some of the properties. We can achieve this by iterating through the list and putting several conditional statements. However, It might be hectic if we want to sort the list based on more than one property with multiple values for each property.

Image for post

Photo Courtesy: Google

LINQ is a life saviour in such a situation and can save our day. We can achieve the same using very minimal lines of code. We will define the lists for the properties through which we want to sort the array and assign the expected value in the sorted order. Then we will use LINQ to iterate and sort the main list.

Here is the link to the fiddle for working example.

Let’s start by creating a model class for our example.

public class Student {
    public string name {get; set;}
    public string standard { get; set;}
    public string section {get; set;}
    public int rollNumber {get; set;}

Each student will have a name along with other information such as the standard they are studying on, section and their roll number.

#linq #sort #c-sharp-programming #dotnet #lists #c#

How to sort list in C# with other sorted lists using LinQ
1.90 GEEK