---
title: "How to sort elements in a collection using the LINQ extension methods?"  
description: "How to sort elements in a collection using the LINQ extension methods?"  
author: "Revati S Misra"  
published: 2023-11-03  
updated: 2023-11-05  
canonical: https://www.mindstick.com/forum/160389/how-to-sort-elements-in-a-collection-using-the-linq-extension-methods  
category: "c#"  
tags: ["c#", "linq", "collection"]  
reading_time: 2 minutes  

---

# How to sort elements in a collection using the LINQ extension methods?

How to sort [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) in a [collection](https://www.mindstick.com/articles/1718/collections-in-java) using the [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries) [extension methods](https://www.mindstick.com/interview/22976/what-is-an-extension-methods-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

You can sort elements in a collection using LINQ [extension](https://www.mindstick.com/articles/22/extension-method) [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) like **OrderBy**, **OrderByDescending**, and **ThenBy** for additional sorting criteria. Here's how to do it:

Assuming you have a collection of objects and want to sort them based on a specific property, follow these steps:

Import the LINQ namespace at the beginning of your code:

```plaintext
using System.Linq;
```

Use the LINQ **OrderBy** or **OrderByDescending** extension methods to perform the initial sorting. Here's an example using **OrderBy** to sort a collection of objects by a property called "Name" in ascending order:

```plaintext
var sortedCollection = originalCollection.OrderBy(item => item.Name);
```

- **item** is a lambda variable that represents each element in the collection.
- **item.Name** is the property you want to sort by.

If you need to sort by multiple criteria, use the **ThenBy** or **ThenByDescending** extension methods after the initial sorting. Here's an example using **ThenBy** to sort by a second property (e.g., "Age") in ascending order:

```plaintext
var sortedCollection = originalCollection
    .OrderBy(item => item.Name)
    .ThenBy(item => item.Age);
```

You can use **ThenByDescending** for descending sorting based on additional criteria.

If you want to sort the collection in place, you can use the **ToList()** method to convert the sorted sequence back to a list:

```plaintext
var sortedList = sortedCollection.ToList();
```

This step is optional; you can keep the sorted collection as an IEnumerable if you don't need to modify it.

Here's a complete example that demonstrates sorting a collection of objects by name and age using LINQ extension methods:

```plaintext
using System;
using System.Linq;
using System.Collections.Generic;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program
{
    public static void Main()
    {
        List<Person> people = new List<Person>
        {
            new Person { Name = "Alice", Age = 30 },
            new Person { Name = "Bob", Age = 25 },
            new Person { Name = "Charlie", Age = 25 },
        };

        var sortedCollection = people
            .OrderBy(person => person.Name)
            .ThenBy(person => person.Age);

        foreach (var person in sortedCollection)
        {
            Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
        }
    }
}
```

This code sorts a list of **Person** objects first by name and then by age, and it prints the sorted results.


---

Original Source: https://www.mindstick.com/forum/160389/how-to-sort-elements-in-a-collection-using-the-linq-extension-methods

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
