---
title: "How to sort class list by integer property?"  
description: "How to sort class list by integer property?"  
author: "Revati S Misra"  
published: 2023-08-18  
updated: 2023-08-19  
canonical: https://www.mindstick.com/forum/159573/how-to-sort-class-list-by-integer-property  
category: "c#"  
tags: ["c#", "sorting", "class"]  
reading_time: 2 minutes  

---

# How to sort class list by integer property?

How to sort [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) list by [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) [property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

Sure, here is how to sort a class list by integer property in C#:

C#

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

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

// Sort the list by the Age property.
people.Sort(delegate (Person p1, Person p2) {
    return p1.Age - p2.Age;
});
```

In this example, we first create a class called `Person` with an integer property called `Age`. We then create a list of `Person` objects and add three objects to the list. The first object has an age of 10, the second object has an age of 20, and the third object has an age of 30.

Next, we use the `Sort()` method to sort the list by the `Age` property. The `Sort()` method takes a delegate as its parameter. The delegate is a function that takes two objects of the same type and returns an integer. In this case, the delegate compares the ages of the two objects and returns the difference.

The `Sort()` method will sort the list in ascending order by default. If you want to sort the list in descending order, you can pass `true` as the second parameter to the `Sort()` method.


---

Original Source: https://www.mindstick.com/forum/159573/how-to-sort-class-list-by-integer-property

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
