自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

講述C# List排序用法的小細(xì)節(jié)

開發(fā) 后端
這里將介紹C# List排序的一些小用法,包括自定義類數(shù)組或List進(jìn)行排序,希望本文能對大家有所幫助。
C# List排序一般用到的是繼承IComparer<T>接口,實(shí)現(xiàn)int IComparer<T>.Compare(T t1, T t2)方法。一般用到C# List排序的地方都比較多。

由于項(xiàng)目的原因用到了List<T> 泛型,F(xiàn)ramework都已經(jīng)到了3.5了??墒俏乙恢倍紱]有正式的用過2.0很是遺憾。

特別是對泛型更是一知半解,今天又弄了些資料覺得挺有用就收集到博客上來了。

閑話少敘,今天用到的List<T>的Sort功能純屬是從高人那里得來的,只是進(jìn)行了少量的改動而已。

要對自定義類數(shù)組或List進(jìn)行排序,譬如:

List<User> userList;

ArrayList arrayList;

最重要的是:繼承IComparer<T>接口,實(shí)現(xiàn)int IComparer<T>.Compare(T t1, T t2)方法。

代碼如下:

  1. /**//// <summary>  
  2. /// 繼承IComparer<T>接口,實(shí)現(xiàn)同一自定義類型 對象比較  
  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. /// 構(gòu)造函數(shù)  
  12. /// </summary>  
  13. /// <param name="type">進(jìn)行比較的類類型</param>  
  14. /// <param name="name">進(jìn)行比較對象的屬性名稱</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. /// 構(gòu)造函數(shù)  
  26. /// </summary>  
  27. /// <param name="className">進(jìn)行比較的類名稱</param>  
  28. /// <param name="name">進(jìn)行比較對象的屬性名稱</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. /// 構(gòu)造函數(shù)  
  44. /// </summary>  
  45. /// <param name="t">進(jìn)行比較的類型的實(shí)例</param>  
  46. /// <param name="name">進(jìn)行比較對象的屬性名稱</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. //必須!實(shí)現(xiàn)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. //交換操作數(shù)  
  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;  

上面主要是運(yùn)用了C#的反射 和 Framework中的排序算法。

像上面那樣實(shí)現(xiàn)接口后,就可以使用List<T>進(jìn)行升序/降序 排序了。

測試代碼如下:

  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. /// 實(shí)體類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,也是生硬的套用高人的代碼,難免會有錯誤。

還請各位指正。謝謝先。

【編輯推薦】

  1. C# Attribute的概念與使用淺析
  2. C# AttributeUsage的使用淺析
  3. 淺析Attribute在C# WinForm控件開發(fā)中的使用
  4. 淺談C#控件屬性串行化的實(shí)現(xiàn)
  5. C#實(shí)例詳解TypeConverterAttribute應(yīng)用
責(zé)任編輯:彭凡 來源: CSDN
相關(guān)推薦

2009-09-27 10:46:30

C#控件數(shù)組

2009-09-27 11:14:09

C#數(shù)組

2024-09-18 05:35:00

LINQC#

2011-05-23 13:27:53

2009-09-18 09:35:36

C# CLR

2009-09-07 16:13:56

C# MessageB

2009-07-30 15:24:13

C#析構(gòu)函數(shù)C#構(gòu)造函數(shù)

2009-10-26 09:50:11

C#與VB.NET

2009-08-20 17:17:02

C#哈希表

2009-09-10 16:30:11

C#排序函數(shù)

2024-09-18 08:00:05

C#編程

2009-08-03 17:38:12

排序算法C#數(shù)據(jù)結(jié)構(gòu)

2011-07-06 10:47:52

C#using

2009-08-26 14:48:21

C#打印分頁

2009-08-26 14:01:33

C# using用法

2009-08-26 18:10:44

C# using的用法

2009-08-26 18:13:55

C#多線程lock

2011-04-25 14:42:10

C#lock

2009-08-10 16:19:37

C#冒泡排序

2009-09-14 18:11:23

C#排序方法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號