由于项目的原因用到了List
特别是对泛型更是一知半解,今天又弄了些资料觉得挺有用就收集到博客上来了。
闲话少叙,今天用到的List
要对自定义类数组或List进行排序,譬如:
List
ArrayList arrayList;
最重要的是:继承IComparer
代码如下:
- /**////
- /// 继承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;
- }
上面主要是运用了C#的反射 和 Framework中的排序算法。
像上面那样实现接口后,就可以使用List
测试代码如下:
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Reflection;
- using System.Text;
- namespace List_T_SortTest_u_2
- ...{
测试Reverser代码段#region 测试Reverser
- /**////
- /// 实体类User,测试用
- ///
- 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; }
- }
- }
- /**////
- /// 主程序类(启动类),测试用
- ///
- 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
- }
以上C# List排序全部完成!另外,各位观众,小弟刚开始接触Framework2.0,也是生硬的套用高人的代码,难免会有错误。
还请各位指正。谢谢先。
【编辑推荐】
下一篇:LINQ嵌套查询实现浅析