标签:规则 委托 ali case *** main eric item argument
先来看看下面List<T>泛型集合的排序例子:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CustomerSort 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 List<int> list = new List<int>(); 14 list.Add(1); 15 list.Add(5); 16 list.Add(2); 17 list.Add(6); 18 list.Add(3); 19 list.Add(4); 20 Console.WriteLine("*****排序前*****"); 21 foreach (var item in list) 22 { 23 Console.WriteLine(item.ToString()); 24 } 25 26 list.Sort(); 27 Console.WriteLine("*****排序后*****"); 28 foreach (var item in list) 29 { 30 Console.WriteLine(item.ToString()); 31 } 32 33 Console.ReadKey(); 34 } 35 } 36 }
输出结果:
从上面的截图中可以看出,Sort()方法默认按照元素的大小进行从小到大的排序,为什么调用Sort()方法就能按照元素的大小进行从小到大的排序呢?其实现原理是什么呢?我们能不能自定义排序规则呢?带着这些问题,我们先来看看Sort()方法的定义,在Sort()方法上面按F12转到定义:
从截图中可以看出,Sort()方法使用了几个重载的方法。可以传递给它的参数有泛型委托Comparison<T> comparison和泛型接口IComparer<T> comparer,以及一个范围值和泛型接口IComparer<T> comparer。只有集合中的元素实现了IComparable<T>接口,才能使用不带参数的Sort()方法。我们在这里实现IComparer<T>接口来创建一个自定义类型的排序功能。
1、定义一个Student类,包括姓名和分数两个属性,可以按照姓名或分数进行排序,Student类定义如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CustomerSort 8 { 9 public class Student 10 { 11 public string Name { get; set; } 12 13 public double Score { get; set; } 14 } 15 }
2、在定义一个枚举,表示排序的种类,即是按照Name排序还是按照Score排序:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CustomerSort 8 { 9 /// <summary> 10 /// 排序的种类 11 /// </summary> 12 public enum CompareType 13 { 14 Name, 15 Score 16 } 17 }
3、实现IComparer接口
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CustomerSort 8 { 9 /// <summary> 10 /// StudentComparer自定义排序规则类实现IComparable接口 11 /// </summary> 12 public class StudentComparer : IComparer<Student> 13 { 14 private CompareType _compareType; 15 16 /// <summary> 17 /// 通过构造函数给_compareType赋值 18 /// </summary> 19 /// <param name="compareType"></param> 20 public StudentComparer(CompareType compareType) 21 { 22 _compareType = compareType; 23 } 24 25 /// <summary> 26 /// 实现IComparer接口的Compare 27 /// </summary> 28 /// <param name="other"></param> 29 /// <returns></returns> 30 public int Compare(Student x, Student y) 31 { 32 if (x == null && y == null) 33 { 34 return 0; 35 } 36 if (x == null) 37 { 38 return -1; 39 } 40 if (y == null) 41 { 42 return 1; 43 } 44 switch (_compareType) 45 { 46 case CompareType.Name: 47 return string.Compare(x.Name, y.Name); 48 break; 49 case CompareType.Score: 50 return x.Score.CompareTo(y.Score); 51 break; 52 default: 53 throw new ArgumentException("无效的比较类型"); 54 } 55 } 56 } 57 }
4、在Main()方法中调用:
先按照Name进行排序:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CustomerSort 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //List<int> list = new List<int>(); 14 //list.Add(1); 15 //list.Add(5); 16 //list.Add(2); 17 //list.Add(6); 18 //list.Add(3); 19 //list.Add(4); 20 //Console.WriteLine("*****排序前*****"); 21 //foreach (var item in list) 22 //{ 23 // Console.WriteLine(item.ToString()); 24 //} 25 26 //list.Sort(); 27 //Console.WriteLine("*****排序后*****"); 28 //foreach (var item in list) 29 //{ 30 // Console.WriteLine(item.ToString()); 31 //} 32 33 34 List<Student> list = new List<Student>() 35 { 36 new Student() 37 { 38 Name="Tom", 39 Score=98 40 } , 41 new Student() 42 { 43 Name="Kevin", 44 Score=69 45 } , 46 new Student() 47 { 48 Name="Leo", 49 Score=81 50 } 51 }; 52 Console.WriteLine("*****排序前*****"); 53 foreach (var item in list) 54 { 55 Console.WriteLine(item.Name); 56 } 57 list.Sort(new StudentComparer(CompareType.Name)); 58 Console.WriteLine("*****排序后*****"); 59 foreach (var item in list) 60 { 61 Console.WriteLine(item.Name); 62 } 63 64 //Console.WriteLine("***按照Score排序***"); 65 66 Console.ReadKey(); 67 } 68 } 69 }
结果:
在按照Score进行排序:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace CustomerSort 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 //List<int> list = new List<int>(); 14 //list.Add(1); 15 //list.Add(5); 16 //list.Add(2); 17 //list.Add(6); 18 //list.Add(3); 19 //list.Add(4); 20 //Console.WriteLine("*****排序前*****"); 21 //foreach (var item in list) 22 //{ 23 // Console.WriteLine(item.ToString()); 24 //} 25 26 //list.Sort(); 27 //Console.WriteLine("*****排序后*****"); 28 //foreach (var item in list) 29 //{ 30 // Console.WriteLine(item.ToString()); 31 //} 32 33 34 List<Student> list = new List<Student>() 35 { 36 new Student() 37 { 38 Name="Tom", 39 Score=98 40 } , 41 new Student() 42 { 43 Name="Kevin", 44 Score=69 45 } , 46 new Student() 47 { 48 Name="Leo", 49 Score=81 50 } 51 }; 52 //Console.WriteLine("*****排序前*****"); 53 //foreach (var item in list) 54 //{ 55 // Console.WriteLine(item.Name); 56 //} 57 //list.Sort(new StudentComparer(CompareType.Name)); 58 //Console.WriteLine("*****排序后*****"); 59 //foreach (var item in list) 60 //{ 61 // Console.WriteLine(item.Name); 62 //} 63 64 Console.WriteLine("*****排序前*****"); 65 foreach (var item in list) 66 { 67 Console.WriteLine(item.Score); 68 } 69 list.Sort(new StudentComparer(CompareType.Name)); 70 Console.WriteLine("*****排序后*****"); 71 foreach (var item in list) 72 { 73 Console.WriteLine(item.Score); 74 } 75 76 Console.ReadKey(); 77 } 78 } 79 }
结果:
标签:规则 委托 ali case *** main eric item argument
原文地址:https://www.cnblogs.com/dotnet261010/p/9278851.html