The following introduces the usage and comparison of sorts of various Lists
First, we create a People entity with attributes of name, age, and sex. The fields we want to sort are age age
Create a new entity class
public class People { public string name { get; set; } public int age { get; set; } public string sex { get; set; } }
Create new list data
List<People> peoples = new List<People>() { new People() {age = 11, name="alun", sex = "male"}, new People() {age=25, name = "Chen Jingtao", sex = "male"}, new People() {age=9, name = "Hui'an", sex = "male"}, new People() {age = 45, name = "Small Ticket", sex = "female"}, new People() {age=3, name = "Xiaoou", sex = "female"}, new People() {age=70, name = "Wangmo", sex = "male"} };
1. The first sorting method, using IComparer
public class PeopleAgeComparer : IComparer<People> { public int Compare(People p1, People p2) { return (); } } (new PeopleAgeComparer());
You can see that the first method is troublesome to compare prices. You need to create a new class to do it.
2. The second sorting method, using delegate to sort
(delegate (People p1, People p2) { return (); });
It is very convenient to see the delegation method, so there is no need to create a new class as troublesome.
3. The second sorting method, using Lambda expressions to sort
( (a, b) => () );
There are three ways to visually sort. I personally think that Lambda expressions are more convenient to use.
I hope this article can help you, thank you for your support for this website!