There are two ways to sort a list by a property in an object in C#:
Using the Sort() method with a Comparison<T> delegate.
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#
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#
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.
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.
There are two ways to sort a list by a property in an object in C#:
Sort()method with aComparison<T>delegate.OrderBy()method from LINQ.Using the
Sort()method with aComparison<T>delegateThe
Sort()method takes aComparison<T>delegate as its parameter. TheComparison<T>delegate is a function that takes two elements of typeTand returns an integer value indicating their relative order. The following code sorts a list ofPersonobjects by their age:C#
In this code, the
Comparison<T>delegate is an anonymous method that compares the ages of twoPersonobjects. TheCompareTo()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 LINQThe
OrderBy()method from LINQ is a more concise way to sort a list by a property. TheOrderBy()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 ofPersonobjects as the previous example using theOrderBy()method:C#
In this code, the lambda expression specifies that the list should be sorted by the
Ageproperty of thePersonobjects.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, theOrderBy()method is more concise and easier to read.