You can sort elements in a collection using LINQ extensionmethods 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:
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:
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:
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:
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:
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
You can sort elements in a collection using LINQ extension methods 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:
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:
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:
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:
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:
This code sorts a list of Person objects first by name and then by age, and it prints the sorted results.