您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關C#中有哪些List排序方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
ArrayList arrayList;
最重要的是:繼承IComparer<T>接口,實現int IComparer<T>.Compare(T t1, T t2)方法。
代碼如下:
/**//// <summary> /// 繼承IComparer<T>接口,實現同一自定義類型 對象比較 /// </summary> /// <typeparam name="T">T為泛用類型</typeparam> public class Reverser<T> : IComparer<T> ...{ private Type type = null; private ReverserInfo info; /**//// <summary> /// 構造函數 /// </summary> /// <param name="type">進行比較的類類型</param> /// <param name="name">進行比較對象的屬性名稱</param> /// <param name="direction">比較方向(升序/降序)</param> public Reverser(Type type, string name, ReverserInfo.Direction direction) ...{ this.type = type; this.info.name = name; if (direction != ReverserInfo.Direction.ASC) this.info.direction = direction; } /**//// <summary> /// 構造函數 /// </summary> /// <param name="className">進行比較的類名稱</param> /// <param name="name">進行比較對象的屬性名稱</param> /// <param name="direction">比較方向(升序/降序)</param> public Reverser(string className, string name, ReverserInfo.Direction direction) ...{ try ...{ this.type = Type.GetType(className, true); this.info.name = name; this.info.direction = direction; } catch (Exception e)...{ throw new Exception(e.Message); } } /**//// <summary> /// 構造函數 /// </summary> /// <param name="t">進行比較的類型的實例</param> /// <param name="name">進行比較對象的屬性名稱</param> /// <param name="direction">比較方向(升序/降序)</param> public Reverser(T t, string name, ReverserInfo.Direction direction) ...{ this.type = t.GetType(); this.info.name = name; this.info.direction = direction; } //必須!實現IComparer<T>的比較方法。 int IComparer<T>.Compare(T t1, T t2) ...{ object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null); object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null); if (this.info.direction != ReverserInfo.Direction.ASC) Swap(ref x, ref y); return (new CaseInsensitiveComparer()).Compare(x, y); } //交換操作數 private void Swap(ref object x, ref object y) ...{ object temp = null; temp = x; x = y; y = temp; } } /**//// <summary> /// 對象比較時使用的信息類 /// </summary> public struct ReverserInfo ...{ /**//// <summary> /// 比較的方向,如下: /// ASC:升序 /// DESC:降序 /// </summary> public enum Direction ...{ ASC = 0, DESC, }; public enum Target ...{ CUSTOMER = 0, FORM, FIELD, SERVER, }; public string name; public Direction direction; public Target target; }
上面主要是運用了C#的反射 和 Framework中的排序算法。
像上面那樣實現接口后,就可以使用List<T>進行升序/降序 排序了。
測試代碼如下:
using System; using System.Collections.Generic; using System.Collections; using System.Reflection; using System.Text; namespace List_T_SortTest_u_2 ...{
測試Reverser代碼段#region 測試Reverser<T>代碼段
/**//// <summary> /// 實體類User,測試用 /// </summary> public class User ...{ protected string _name; protected int _age; protected string _address; public User(string name, int age, string address) ...{ this._name = name; this._age = age; this._address = address; } public string Name ...{ get ...{ return _name; } set ...{ _name = value; } } public int Age ...{ get ...{ return _age; } set ...{ _age = value; } } public string Address ...{ get ...{ return _address; } set ...{ _address = value; } } } /**//// <summary> /// 主程序類(啟動類),測試用 /// </summary> class Program ...{ static void Main(string[] args) ...{ List<User> userList = new List<User>(); User user; user = new User("Wang", 21, "ShenYang"); userList.Add(user); user = new User("Yan", 27, "JinZhou"); userList.Add(user); user = new User("Liu", 26, "BeiJing"); userList.Add(user); user = new User("Zhao", 30, "ChaoYang"); userList.Add(user); user = new User("Yang", 27, "FuXin"); userList.Add(user); //for (int i = 0; i < ar.Count; i++ ) // ; Console.Write("Name "); Console.Write("Age "); Console.Write("Address " + " " + " "); Console.WriteLine("-----------------------"); foreach (User u in userList) ...{ Console.Write(u.Name + " "); Console.Write(u.Age + " "); Console.Write(u.Address + " " + " "); } Console.WriteLine(); Reverser<User> reverser = new Reverser<User>(user.GetType(), "Name", ReverserInfo.Direction.DESC); userList.Sort(reverser); Console.WriteLine(); foreach (User u in userList) ...{ Console.Write(u.Name + " "); Console.Write(u.Age + " "); Console.Write(u.Address + " " + " "); } Console.WriteLine(); reverser = new Reverser<User>(user.GetType(), "Age", ReverserInfo.Direction.ASC); userList.Sort(reverser); Console.WriteLine(); foreach (User u in userList) ...{ Console.Write(u.Name + " "); Console.Write(u.Age + " "); Console.Write(u.Address + " " + " "); } Console.Read(); } } #endregion }
看完上述內容,你們對C#中有哪些List排序方法有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。