Method tutorial for custom sorting of list generics
This article mainly explains the "list generic custom sorting method tutorial", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn the "list generic custom sorting method tutorial" bar!
The copy code is as follows:
Static void Main (string [] args)
{
Employee employee = new Employee ()
/ / set the initial value
List employeeList = new List ()
EmployeeList.Add (new Employee () {EmpId = "001", EmpName = "Tony"})
EmployeeList.Add (new Employee () {EmpId = "002", EmpName = "Mack"})
EmployeeList.Add (new Employee () {EmpId = "003", EmpName = "Jon"})
EmployeeList.Add (new Employee () {EmpId = "004", EmpName =" Dawei "})
EmployeeList.Add (new Employee () {EmpId = "005", EmpName = "Jack"})
EmployeeList.Add (new Employee () {EmpId = "006", EmpName =" Abby "})
EmployeeList.Add (new Employee () {EmpId = "007", EmpName = "Carrie"})
/ / specify pre-sort elements
List toSortList = new List ()
ToSortList.Add (new Employee () {EmpId = "003", EmpName = "Jon"})
ToSortList.Add (new Employee () {EmpId = "005", EmpName = "Jack"})
ToSortList.Add (new Employee () {EmpId = "007", EmpName = "Carrie"})
/ / Custom sort delegation
EmployeeList.Sort ((Employee x, Employee y) = > (toSortList.Count (e = > e.EmpId = = y.EmpId)-toSortList.Count (e = > e.EmpId = = x.EmpId)
}
Public class Employee
{
Public string EmpId
{
Get
Set
}
Public string EmpName
{
Get
Set
}
}
After sorting, the elements in the original list are sorted according to 001jue 002j003, and the elements are sorted as 003005007001002004. Rank the specified 003, 005, 007 at the front of the List
Thank you for your reading, the above is the content of the "list generic Custom sorting method tutorial". After the study of this article, I believe you have a deeper understanding of the list generic Custom sorting method tutorial, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!