List<T>泛型数组API自带的Sort() 排序方法根据自定义排序实现
2012-08-10 16:45
1071 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace BLL
{
public class OrderHelper
{
}
/// <summary>
/// 继承IComparer<T>接口,实现同一自定义类型 对象比较
/// </summary>
/// <typeparam >T为泛用类型</typeparam>
public class Reverser<T> : IComparer<T>
{
private Type type = null;
private ReverserInfo info;
/**/
/// <summary>
/// 构造函数
/// </summary>
/// <param >进行比较的类类型</param>
/// <param >进行比较对象的属性名称</param>
/// <param >比较方向(升序/降序)</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 >进行比较的类名称</param>
/// <param >进行比较对象的属性名称</param>
/// <param >比较方向(升序/降序)</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 >进行比较的类型的实例</param>
/// <param >进行比较对象的属性名称</param>
/// <param >比较方向(升序/降序)</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;
}
}
(二)应用sort()方法:
AgreementSheet abc = new AgreementSheet(); //new 一个当前处理的类
Reverser<AgreementSheet> reverser = new Reverser<AgreementSheet>(abc.GetType(), "Customer Name in GSX", ReverserInfo.Direction.DESC);
//"Customer Name in GSX":指定我们List<>泛型 类中一个要排序的列
list.Sort(reverser); //即已经完成自定义排序了
希望能帮助大家,谢谢!
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
namespace BLL
{
public class OrderHelper
{
}
/// <summary>
/// 继承IComparer<T>接口,实现同一自定义类型 对象比较
/// </summary>
/// <typeparam >T为泛用类型</typeparam>
public class Reverser<T> : IComparer<T>
{
private Type type = null;
private ReverserInfo info;
/**/
/// <summary>
/// 构造函数
/// </summary>
/// <param >进行比较的类类型</param>
/// <param >进行比较对象的属性名称</param>
/// <param >比较方向(升序/降序)</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 >进行比较的类名称</param>
/// <param >进行比较对象的属性名称</param>
/// <param >比较方向(升序/降序)</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 >进行比较的类型的实例</param>
/// <param >进行比较对象的属性名称</param>
/// <param >比较方向(升序/降序)</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;
}
}
(二)应用sort()方法:
AgreementSheet abc = new AgreementSheet(); //new 一个当前处理的类
Reverser<AgreementSheet> reverser = new Reverser<AgreementSheet>(abc.GetType(), "Customer Name in GSX", ReverserInfo.Direction.DESC);
//"Customer Name in GSX":指定我们List<>泛型 类中一个要排序的列
list.Sort(reverser); //即已经完成自定义排序了
希望能帮助大家,谢谢!
相关文章推荐
- 数组排序方法的性能比较(2):Array.Sort<T>实现分析
- <转>根据泛型生成数组 与数组 List相互转换的方法
- 艾伟_转载:数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- 数组排序方法的性能比较(2):Array.Sort<T>实现分析
- .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法(转)
- C#泛型集合List<T>自带方法之排序
- 一起谈.NET技术,数组排序方法的性能比较(中):Array.Sort<T> 实现分析
- .NET中string[]数组和List<string>泛型的相互转换以及Array类的Sort()方法 【整理】
- 实现List<>自定义排序
- 实体类集合(List<Object>)根据实体类的一个或者多个属性对集合进行排序-Collections.sort
- java Collections.sort()实现List排序的默认方法和自定义方法
- C# 数组集合 <一>基本概念(Array&ArrayList&List<T>&SortList&LinkList的实现Code)
- java Collections.sort()实现List排序的默认方法和自定义方法
- C#中对List<Object>泛型集合,根据对象的某一属性进行升序排序
- C#基础教程之IComparable用法,实现List<T>.sort()排序
- List<T> 排序(Sort)查找(Find)功能的多种实现
- Java List<>排序——重载Collections.sort方法
- List<T>泛型集合根据对象的某一属性排序
- java Collections.sort()实现List排序的默认方法和自定义方法
- java编程实现对10个整型数据元素输出最小的两个数,java自带的Arrays.sort排序方法,小--->大