讲述C 讲述cia的美剧有哪些
创始人
2024-06-07 00:30:31
0
C# List排序一般用到的是继承IComparer接口,实现int IComparer.Compare(T t1, T t2)方法。一般用到C# List排序的地方都比较多。

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

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

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

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

List userList;

ArrayList arrayList;

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

代码如下:

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

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

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

测试代码如下:

  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代码段

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

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

还请各位指正。谢谢先。

【编辑推荐】

  1. C# Attribute的概念与使用浅析
  2. C# AttributeUsage的使用浅析
  3. 浅析Attribute在C# WinForm控件开发中的使用
  4. 浅谈C#控件属性串行化的实现
  5. C#实例详解TypeConverterAttribute应用

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...