How to Sort a List<T> by a property in the object in C#

How to Sort a List<T> by a property in the object in C#

You can sort a List<T> by a property in the objects using the List<T>.Sort method and a custom comparison function that compares the property you want to sort by. Here's an example:

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

List<Person> people = new List<Person>()
{
    new Person { Name = "Alice", Age = 25 },
    new Person { Name = "Bob", Age = 30 },
    new Person { Name = "Charlie", Age = 20 }
};

people.Sort((person1, person2) => person1.Age.CompareTo(person2.Age));

In this example, we define a Person class with Name and Age properties. We then create a List<Person> called people and populate it with three Person objects.

To sort the list by Age, we call the Sort method on the List<Person> object and pass in a lambda function that compares the Age property of two Person objects. The lambda function takes two Person objects as input and returns an integer that indicates their relative order. In this case, we use the CompareTo method of the int type to compare the Age properties.

After the Sort method has been called, the people list will be sorted in ascending order by the Age property. If you want to sort the list in descending order, you can reverse the comparison result:

people.Sort((person1, person2) => person2.Age.CompareTo(person1.Age));

Note that the Sort method modifies the original list. If you want to create a sorted copy of the list without modifying the original, you can use LINQ's OrderBy or OrderByDescending method to create a new sorted list:

List<Person> sortedPeople = people.OrderBy(person => person.Age).ToList();

Examples

  1. "C# sort List<T> by property"

    • Description: Learn how to sort a List<T> by a specific property in the objects.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
      
  2. "Sort List<T> in C# by property ascending"

    • Description: Implement code to sort a List<T> by a specific property in ascending order.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
      
  3. "C# List<T> sorting by property example"

    • Description: Explore an example of sorting a List<T> by a specific property in C#.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
      
  4. "Sort List<T> by property in C# descending order"

    • Description: Learn how to sort a List<T> by a specific property in descending order using C#.
    • Code:
      List<YourClass> sortedList = yourList.OrderByDescending(x => x.PropertyName).ToList();
      
  5. "C# List<T> sort by property descending"

    • Description: Implement code to sort a List<T> by a specific property in descending order.
    • Code:
      List<YourClass> sortedList = yourList.OrderByDescending(x => x.PropertyName).ToList();
      
  6. "Sorting List<T> by property in C# code"

    • Description: Access code snippets for sorting a List<T> by a specific property in C#.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
      
  7. "C# List<T> sorting by property code example"

    • Description: Find a concise code example for sorting a List<T> by a specific property in C#.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
      
  8. "List<T> sorting in C# with LINQ by property"

    • Description: Explore how to use LINQ to sort a List<T> by a specific property in C#.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
      
  9. "Sort List<T> by multiple properties in C#"

    • Description: Learn how to sort a List<T> by multiple properties in C#.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.Property1).ThenBy(x => x.Property2).ToList();
      
  10. "C# List<T> sorting by property with custom comparer"

    • Description: Implement custom sorting logic for a List<T> by a specific property in C# using a comparer.
    • Code:
      List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName, new YourCustomComparer()).ToList();
      

More Tags

multimarkdown samsung-galaxy polymorphism pyglet model-validation mayavi lldb supervised-learning eloquent-relationship file-transfer

More C# Questions

More Auto Calculators

More Gardening and crops Calculators

More Bio laboratory Calculators

More Cat Calculators