When sorting collections, the first thing that comes to mind is to use the OrderBy method.
class Program { static void Main(string[] args) { IEnumerable<Student> result = GetStudents().OrderBy(r => ); foreach (var item in result) { ( + "--" + ); } (); } private static List<Student> GetStudents() { return new List<Student>() { new Student(){Id = 1, Name = "Zhang San",Age = 15, Score = 80}, new Student(){Id = 2, Name = "Li Si",Age = 16, Score = 70}, new Student(){Id = 3, Name = "Zhao Wu",Age = 14, Score = 90} }; } } public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } }
Above, the type returned by OrderBy is IEnumerable<Student>.
If you want to use the Sort method of List<T>, you need to let Student implement the IComparable<Student> interface.
class Program { static void Main(string[] args) { List<Student> result = GetStudents(); (); foreach (var item in result) { ( + "--" + ); } (); } private static List<Student> GetStudents() { return new List<Student>() { new Student(){Id = 1, Name = "Zhang San",Age = 15, Score = 80}, new Student(){Id = 2, Name = "Li Si",Age = 16, Score = 70}, new Student(){Id = 3, Name = "Zhao Wu",Age = 14, Score = 90} }; } } public class Student : IComparable<Student> { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } public int CompareTo(Student other) { return (); } }
It is good to have Student implement the IComparable<Student> interface. If Student is a sealed class, what if we can't make it implement the IComparable<Student> interface? Don't worry, the Sort method provides an overload that can receive IComparer interface types.
class Program { static void Main(string[] args) { List<Student> result = GetStudents(); (new StudentSorter()); foreach (var item in result) { ( + "--" + ); } (); } private static List<Student> GetStudents() { return new List<Student>() { new Student(){Id = 1, Name = "Zhang San",Age = 15, Score = 80}, new Student(){Id = 2, Name = "Li Si",Age = 16, Score = 70}, new Student(){Id = 3, Name = "Zhao Wu",Age = 14, Score = 90} }; } } public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public int Score { get; set; } } public class StudentSorter : IComparer<Student> { public int Compare(Student x, Student y) { return (); } }
In summary, if we want to sort a collection, there are roughly three ways:
1. Use the OrderBy method to return the IEnumerable<T> type.
2. Let the collection elements implement the IComparable<T> interface, and then use the Sort method to return to void.
3. The collection element does not implement the IComparable<T> interface. Write a class that implements the IComparer<T> interface for the collection element type, and use this class instance as a parameter of the Sort method.
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links