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

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

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.

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#

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Is Using LINQ in C# Bad for Performance?

LINQ (Language INtegrated Query) is a set of features in C# for concise and declarative code. I use it pretty much wherever I can, but sometimes I wonder and people ask: is this a performance trap? It would be painful to have to replace many usages of LINQ with other constructs in addition to time spent finding that to be an actual bottleneck. Let’s do a basic benchmark to see what using LINQ might cost us compared to more traditional constructs such as for and foreach loops, and see if this worry is valid.

C++ Insertion Sort Example | Insertion Sort Program In C++

C++ Insertion Sort Example | Insertion Sort Program In C++. To sort an array using insertion sort technique in C++ programming, you have to ask to the user to enter the array size and array elements in random order, now start sorting the elements of the array in ascending order using insertion sort technique

C programming for Beginners | Learn C Programming | C Language

C Language is an evergreen language and is used widely across different industries, This C programming is a must for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. Great Learning brings you this live session on "Introduction to C". In this live session, we will be covering major concepts in C Programming such as Different Variables, Different Data Types that are being used, its Operators, Flow control statements, Structure, and lot more.

5 Best C# (C-Sharp) Programming Courses for Beginners in 2020

In this article, I am going to share some of the best courses to learn C# or C-Sharp in 2020. These courses are suitable for both beginners and experienced programmers who want to fill gaps in their C# knowledge.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.