Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2428
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 11
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-19 00:52
文章分类

全部博文(1)

文章存档

2014年(1)

我的朋友
最近访客

分类: C/C++

2014-01-19 01:00:43

如何进行对C# .NET通用泛型进行快速排序?

我们经常使用List泛型进行数据的封装,但是有时候,在某种需求下,你可能需要对这个泛型进行排序,而排序规则是根据model中的某一个属性进行排序,这时棘手的事情来了,怎么办?!!

这个时候我们需要自己扩展一个排序方法,以下我给出一个继承自IComparer接口的类,此类内置好升序和降序的排序规则:
 

   [csharp]
 

   ///
 

   /// 继承IComparer接口,实现同一自定义类型 对象比较
 

   ///

 

   /// T为泛用类型
 

   public class Reverser : IComparer
 

   {
 

   private Type type = null;
 

   private ReverserInfo info;
 

   ///
 

   /// 构造函数
 

   ///

 

   /// 进行比较的类类型
 

   /// 进行比较对象的属性名称
 

   /// 比较方向(升序/降序)
 

   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;
 

   }
 

   ///
 

   /// 构造函数
 

   ///

 

   /// 进行比较的类名称
 

   /// 进行比较对象的属性名称
 

   /// 比较方向(升序/降序)
 

   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);
 

   }
 

   }
///
 

   /// 构造函数
 

   ///

 

   /// 进行比较的类型的实例
 

   /// 进行比较对象的属性名称
 

   /// 比较方向(升序/降序)
 

   public Reverser(T t, string name, ReverserInfo.Direction direction)
 

   {
 

   this.type = t.GetType();
 

   this.info.name = name;
 

   this.info.direction = direction;
 

   }
 

   //必须!实现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);
 

   }
 

   //交换操作数
 

   private void Swap(ref object x, ref object y)
 

   {
 

   object temp = null;
 

   temp = x;
 

   x = y;
 

   y = temp;
 

   }
 

   }
 

   ///
 

   /// 对象比较时使用的信息类
 

   ///

 

   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;
 

   }
 

   ///
 

   /// 继承IComparer接口,实现同一自定义类型 对象比较
 

   ///

 

   /// T为泛用类型
 

   public class Reverser : IComparer
 

   {
 

   private Type type = null;
 

   private ReverserInfo info;
 

   ///
 

   /// 构造函数
 

   ///

 

   /// 进行比较的类类型
 

   /// 进行比较对象的属性名称
 

   /// 比较方向(升序/降序)
 

   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;
 

   }
 

   ///
 

   /// 构造函数
 

   ///

 

   /// 进行比较的类名称
 

   /// 进行比较对象的属性名称
 

   /// 比较方向(升序/降序)
 

   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);
 

   }
 

   }
   ///
 

   /// 构造函数
 

   ///

 

   /// 进行比较的类型的实例
 

   /// 进行比较对象的属性名称
 

   /// 比较方向(升序/降序)
 

   public Reverser(T t, string name, ReverserInfo.Direction direction)
 

   {
 

   this.type = t.GetType();
 

   this.info.name = name;
 

   this.info.direction = direction;
 

   }
 

   //必须!实现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);
 

   }
 

   //交换操作数
 

   private void Swap(ref object x, ref object y)
 

   {
 

   object temp = null;
 

   temp = x;
 

   x = y;
 

   y = temp;
 

   }
 

   }
 

   ///
 

   /// 对象比较时使用的信息类
 

   ///

 

   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;
 

   }
 

   此时写好后,就只需要知道如何调用就行了:
 

   [csharp]
 

   Reverser reverser = new Reverser(typeof(Model), “btnIndex”, ReverserInfo.Direction.ASC);
 

   Reverser reverser = new Reverser(typeof(Model), “btnIndex”, ReverserInfo.Direction.ASC);我们通过这样的调用方式,就得到一个定义好的排序规则 reverser,假设我们的泛型实例名为 list,则按照如下调用:
 

   [csharp]
 

   list.Sort(reverser);
 

   list.Sort(reverser);
 

   至此,就完成了特定的排序规则处理了。
 

   后感:
 

   虽然这种需求不多,但是在自定义构造的泛型中,需要根据某一属性进行有序显示的时候,就非常实用。
 

   同时任何自定义的类组成的泛型集合都可以实现自定义排序,可以做成一个通用类,没有必要每一个自定义类都去实现排序的接口。


PS:转载来源:暗客安全网:原帖地址:
阅读(630) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~