---
title: "How to Sort a List by a property in the object in c#?"  
description: "How to Sort a List by a property in the object in c#?"  
author: "Steilla Mitchel"  
published: 2023-08-17  
updated: 2023-08-18  
canonical: https://www.mindstick.com/forum/159567/how-to-sort-a-list-by-a-property-in-the-object-in-c-sharp  
category: "c#"  
tags: ["c#", "sorting", "list"]  
reading_time: 2 minutes  

---

# How to Sort a List by a property in the object in c#?

How to Sort a List<T> by a [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) in the [object in c#](https://www.mindstick.com/forum/161296/what-is-the-purpose-of-the-history-object-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

There are two ways to sort a list by a property in an object in C#:

1. Using the `Sort()` method with a `Comparison<T>` delegate.
2. Using the `OrderBy()` method from LINQ.

**Using the** `Sort()` **method with a** `Comparison<T>` **delegate**

The `Sort()` method takes a `Comparison<T>` delegate as its parameter. The `Comparison<T>` delegate is a function that takes two elements of type `T` and returns an integer value indicating their relative order. The following code sorts a list of `Person` objects by their age:

C#

```plaintext
public class Person {
    public int Age { get; set; }
}

List<Person> people = new List<Person>();
people.Add(new Person { Age = 20 });
people.Add(new Person { Age = 30 });
people.Add(new Person { Age = 10 });

people.Sort(delegate(Person p1, Person p2) {
    return p1.Age.CompareTo(p2.Age);
});
```

In this code, the `Comparison<T>` delegate is an anonymous method that compares the ages of two `Person` objects. The `CompareTo()` method returns an integer value indicating the relative order of the two objects. A value of -1 means that the first object is less than the second object, a value of 0 means that the two objects are equal, and a value of 1 means that the first object is greater than the second object.

**Using the** `OrderBy()` **method from LINQ**

The `OrderBy()` method from LINQ is a more concise way to sort a list by a property. The `OrderBy()` method takes a lambda expression as its parameter. The lambda expression specifies the property that the list should be sorted by. The following code sorts the same list of `Person` objects as the previous example using the `OrderBy()` method:

C#

```plaintext
var sortedPeople = people.OrderBy(p => p.Age);
```

In this code, the lambda expression specifies that the list should be sorted by the `Age` property of the `Person` objects.

Which method you use to sort a list by a property depends on your preference. The `Sort()` method is more flexible, as you can use it to sort a list by any property. However, the `OrderBy()` method is more concise and easier to read.


---

Original Source: https://www.mindstick.com/forum/159567/how-to-sort-a-list-by-a-property-in-the-object-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
