真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C#中有哪些List排序方法

今天就跟大家聊聊有關(guān)C#中有哪些List排序方法,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

10年積累的網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有疏附免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

ArrayList arrayList;

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

代碼如下:

/**////   /// 繼承IComparer接口,實(shí)現(xiàn)同一自定義類型 對(duì)象比較  ///   /// T為泛用類型  public class Reverser : IComparer  ...{  private Type type = null;  private ReverserInfo info;   /**////   /// 構(gòu)造函數(shù)  ///   /// 進(jìn)行比較的類類型  /// 進(jìn)行比較對(duì)象的屬性名稱  /// 比較方向(升序/降序)  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;  }   /**////   /// 構(gòu)造函數(shù)  ///   /// 進(jìn)行比較的類名稱  /// 進(jìn)行比較對(duì)象的屬性名稱  /// 比較方向(升序/降序)  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);  }  }   /**////   /// 構(gòu)造函數(shù)  ///   /// 進(jìn)行比較的類型的實(shí)例  /// 進(jìn)行比較對(duì)象的屬性名稱  /// 比較方向(升序/降序)  public Reverser(T t, string name, ReverserInfo.Direction direction)  ...{  this.type = t.GetType();  this.info.name = name;  this.info.direction = direction;  }   //必須!實(shí)現(xiàn)IComparer的比較方法。  int IComparer.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);  }   //交換操作數(shù)  private void Swap(ref object x, ref object y)  ...{  object temp = null;  temp = x;  x = y;  y = temp;  }  }   /**////   /// 對(duì)象比較時(shí)使用的信息類  ///   public struct ReverserInfo  ...{  /**////   /// 比較的方向,如下:  /// ASC:升序  /// DESC:降序  ///   public enum Direction  ...{  ASC = 0,  DESC,  };   public enum Target  ...{  CUSTOMER = 0,  FORM,  FIELD,  SERVER,  };   public string name;  public Direction direction;  public Target target;  }

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

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

測(cè)試代碼如下:

using System;  using System.Collections.Generic;  using System.Collections;  using System.Reflection;  using System.Text;   namespace List_T_SortTest_u_2  ...{

測(cè)試Reverser代碼段#region 測(cè)試Reverser代碼段

/**////   /// 實(shí)體類User,測(cè)試用  ///   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; }  }  }   /**////   /// 主程序類(啟動(dòng)類),測(cè)試用  ///   class Program  ...{  static void Main(string[] args)  ...{  List userList = new List();  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 reverser = new Reverser(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.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  }

看完上述內(nèi)容,你們對(duì)C#中有哪些List排序方法有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。


分享名稱:C#中有哪些List排序方法
瀏覽路徑:http://weahome.cn/article/gijdgi.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部