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();
"C# sort List<T> by property"
List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
"Sort List<T> in C# by property ascending"
List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
"C# List<T> sorting by property example"
List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
"Sort List<T> by property in C# descending order"
List<YourClass> sortedList = yourList.OrderByDescending(x => x.PropertyName).ToList();
"C# List<T> sort by property descending"
List<YourClass> sortedList = yourList.OrderByDescending(x => x.PropertyName).ToList();
"Sorting List<T> by property in C# code"
List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
"C# List<T> sorting by property code example"
List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
"List<T> sorting in C# with LINQ by property"
List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName).ToList();
"Sort List<T> by multiple properties in C#"
List<YourClass> sortedList = yourList.OrderBy(x => x.Property1).ThenBy(x => x.Property2).ToList();
"C# List<T> sorting by property with custom comparer"
List<YourClass> sortedList = yourList.OrderBy(x => x.PropertyName, new YourCustomComparer()).ToList();
multimarkdown samsung-galaxy polymorphism pyglet model-validation mayavi lldb supervised-learning eloquent-relationship file-transfer