讲述C# List排序用法的小细节

来源:互联网 发布:手机mac修改软件 编辑:程序博客网 时间:2024/06/02 07:40

C# List排序一般用到的是继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。一般用到C# List排序的地方都比较多。

由于项目的原因用到了List<T> 泛型,Framework都已经到了3.5了。可是我一直都没有正式的用过2.0很是遗憾。

特别是对泛型更是一知半解,今天又弄了些资料觉得挺有用就收集到博客上来了。

闲话少叙,今天用到的List<T>的Sort功能纯属是从高人那里得来的,只是进行了少量的改动而已。

要对自定义类数组或List进行排序,譬如:

List<User> userList;

ArrayList arrayList;

最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。

代码如下:

  1. /**//// <summary>  
  2. /// 继承IComparer<T>接口,实现同一自定义类型 对象比较  
  3. /// </summary>  
  4. /// <typeparam name="T">T为泛用类型</typeparam>  
  5. public class Reverser<T> : IComparer<T>  
  6. ...{  
  7. private Type type = null;  
  8. private ReverserInfo info;  
  9.  
  10. /**//// <summary>  
  11. /// 构造函数  
  12. /// </summary>  
  13. /// <param name="type">进行比较的类类型</param>  
  14. /// <param name="name">进行比较对象的属性名称</param>  
  15. /// <param name="direction">比较方向(升序/降序)</param>  
  16. public Reverser(Type type, string name, ReverserInfo.Direction direction)  
  17. ...{  
  18. this.type = type;  
  19. this.info.name = name;  
  20. if (direction != ReverserInfo.Direction.ASC)  
  21. this.info.direction = direction;  
  22. }  
  23.  
  24. /**//// <summary>  
  25. /// 构造函数  
  26. /// </summary>  
  27. /// <param name="className">进行比较的类名称</param>  
  28. /// <param name="name">进行比较对象的属性名称</param>  
  29. /// <param name="direction">比较方向(升序/降序)</param>  
  30. public Reverser(string className, string name, ReverserInfo.Direction direction) ...{  
  31. try 
  32. ...{  
  33. this.type = Type.GetType(className, true);  
  34. this.info.name = name;  
  35. this.info.direction = direction;  
  36. }  
  37. catch (Exception e)...{  
  38. throw new Exception(e.Message);  
  39. }  
  40. }  
  41.  
  42. /**//// <summary>  
  43. /// 构造函数  
  44. /// </summary>  
  45. /// <param name="t">进行比较的类型的实例</param>  
  46. /// <param name="name">进行比较对象的属性名称</param>  
  47. /// <param name="direction">比较方向(升序/降序)</param>  
  48. public Reverser(T t, string name, ReverserInfo.Direction direction)  
  49. ...{  
  50. this.type = t.GetType();  
  51. this.info.name = name;  
  52. this.info.direction = direction;  
  53. }  
  54.  
  55. //必须!实现IComparer<T>的比较方法。  
  56. int IComparer<T>.Compare(T t1, T t2)  
  57. ...{  
  58. object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);  
  59. object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);  
  60. if (this.info.direction != ReverserInfo.Direction.ASC)  
  61. Swap(ref x, ref y);  
  62. return (new CaseInsensitiveComparer()).Compare(x, y);  
  63. }  
  64.  
  65. //交换操作数  
  66. private void Swap(ref object x, ref object y)  
  67. ...{  
  68. object temp = null;  
  69. temp = x;  
  70. x = y;  
  71. y = temp;  
  72. }  
  73. }  
  74.  
  75. /**//// <summary>  
  76. /// 对象比较时使用的信息类  
  77. /// </summary>  
  78. public struct ReverserInfo  
  79. ...{  
  80. /**//// <summary>  
  81. /// 比较的方向,如下:  
  82. /// ASC:升序  
  83. /// DESC:降序  
  84. /// </summary>  
  85. public enum Direction  
  86. ...{  
  87. ASC = 0,  
  88. DESC,  
  89. };  
  90.  
  91. public enum Target  
  92. ...{  
  93. CUSTOMER = 0,  
  94. FORM,  
  95. FIELD,  
  96. SERVER,  
  97. };  
  98.  
  99. public string name;  
  100. public Direction direction;  
  101. public Target target;  

上面主要是运用了C#的反射 和 Framework中的排序算法。

像上面那样实现接口后,就可以使用List<T>进行升序/降序 排序了。

测试代码如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Collections;  
  4. using System.Reflection;  
  5. using System.Text;  
  6.  
  7. namespace List_T_SortTest_u_2  
  8. ...{ 

测试Reverser代码段#region 测试Reverser<T>代码段

  1. /**//// <summary>  
  2. /// 实体类User,测试用  
  3. /// </summary>  
  4. public class User  
  5. ...{  
  6. protected string _name;  
  7. protected int _age;  
  8. protected string _address;  
  9.  
  10. public User(string name, int age, string address)  
  11. ...{  
  12. this._name = name;  
  13. this._age = age;  
  14. this._address = address;  
  15. }  
  16.  
  17. public string Name  
  18. ...{  
  19. get ...{ return _name; }  
  20. set ...{ _name = value; }  
  21. }  
  22.  
  23. public int Age  
  24. ...{  
  25. get ...{ return _age; }  
  26. set ...{ _age = value; }  
  27. }  
  28.  
  29. public string Address  
  30. ...{  
  31. get ...{ return _address; }  
  32. set ...{ _address = value; }  
  33. }  
  34. }  
  35.  
  36. /**//// <summary>  
  37. /// 主程序类(启动类),测试用  
  38. /// </summary>  
  39. class Program  
  40. ...{  
  41. static void Main(string[] args)  
  42. ...{  
  43. List<User> userList = new List<User>();  
  44. User user;  
  45.  
  46. user = new User("Wang", 21, "ShenYang");  
  47. userList.Add(user);  
  48. user = new User("Yan", 27, "JinZhou");  
  49. userList.Add(user);  
  50. user = new User("Liu", 26, "BeiJing");  
  51. userList.Add(user);  
  52. user = new User("Zhao", 30, "ChaoYang");  
  53. userList.Add(user);  
  54. user = new User("Yang", 27, "FuXin");  
  55. userList.Add(user);  
  56.  
  57. //for (int i = 0; i < ar.Count; i++ )  
  58. //    ;  
  59. Console.Write("Name     ");  
  60. Console.Write("Age      ");  
  61. Console.Write("Address  " + " " + " ");  
  62. Console.WriteLine("-----------------------");  
  63. foreach (User u in userList)  
  64. ...{  
  65. Console.Write(u.Name + "    ");  
  66. Console.Write(u.Age + "    ");  
  67. Console.Write(u.Address + "    " + " ");  
  68. }  
  69. Console.WriteLine();  
  70.  
  71. Reverser<User> reverser = new Reverser<User>(user.GetType(), "Name", ReverserInfo.Direction.DESC);  
  72. userList.Sort(reverser);  
  73. Console.WriteLine();  
  74. foreach (User u in userList)  
  75. ...{  
  76. Console.Write(u.Name + "    ");  
  77. Console.Write(u.Age + "    ");  
  78. Console.Write(u.Address + "    " + " ");  
  79. }  
  80. Console.WriteLine();  
  81.  
  82. reverser = new Reverser<User>(user.GetType(), "Age", ReverserInfo.Direction.ASC);  
  83. userList.Sort(reverser);  
  84. Console.WriteLine();  
  85. foreach (User u in userList)  
  86. ...{  
  87. Console.Write(u.Name + "    ");  
  88. Console.Write(u.Age + "    ");  
  89. Console.Write(u.Address + "    " + " ");  
  90. }  
  91.  
  92. Console.Read();  
  93. }  
  94. }  
  95. #endregion  

以上C# List排序全部完成!另外,各位观众,小弟刚开始接触Framework2.0,也是生硬的套用高人的代码,难免会有错误