C#中List的排序(Sort)
要对自定义类数组或List进行排序,譬如:
List<User> userList;
ArrayList arrayList;
最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。
代码如下:

 /// <summary>
    /// <summary> /// 继承IComparer<T>接口,实现同一自定义类型 对象比较
    /// 继承IComparer<T>接口,实现同一自定义类型 对象比较 /// </summary>
    /// </summary> /// <typeparam name="T">T为泛用类型</typeparam>
    /// <typeparam name="T">T为泛用类型</typeparam> public class Reverser<T> : IComparer<T>
    public class Reverser<T> : IComparer<T> {
    { private Type type = null;
        private Type type = null; private ReverserInfo info;
        private ReverserInfo info;
 /// <summary>
        /// <summary> /// 构造函数
        /// 构造函数 /// </summary>
        /// </summary> /// <param name="type">进行比较的类类型</param>
        /// <param name="type">进行比较的类类型</param> /// <param name="name">进行比较对象的属性名称</param>
        /// <param name="name">进行比较对象的属性名称</param> /// <param name="direction">比较方向(升序/降序)</param>
        /// <param name="direction">比较方向(升序/降序)</param> public Reverser(Type type, string name, ReverserInfo.Direction direction)
        public Reverser(Type type, string name, ReverserInfo.Direction direction) {
        { this.type = type;
            this.type = type; this.info.name = name;
            this.info.name = name; if (direction != ReverserInfo.Direction.ASC)
            if (direction != ReverserInfo.Direction.ASC) this.info.direction = direction;
                this.info.direction = direction; }
        }
 /// <summary>
        /// <summary> /// 构造函数
        /// 构造函数 /// </summary>
        /// </summary> /// <param name="className">进行比较的类名称</param>
        /// <param name="className">进行比较的类名称</param> /// <param name="name">进行比较对象的属性名称</param>
        /// <param name="name">进行比较对象的属性名称</param> /// <param name="direction">比较方向(升序/降序)</param>
        /// <param name="direction">比较方向(升序/降序)</param> public Reverser(string className, string name, ReverserInfo.Direction direction) {
        public Reverser(string className, string name, ReverserInfo.Direction direction) { try
            try {
            { this.type = Type.GetType(className, true);
                this.type = Type.GetType(className, true); this.info.name = name;
                this.info.name = name; this.info.direction = direction;
                this.info.direction = direction; }
            } catch (Exception e){
            catch (Exception e){ throw new Exception(e.Message);
                throw new Exception(e.Message); }
            } }
        }
 /// <summary>
        /// <summary> /// 构造函数
        /// 构造函数 /// </summary>
        /// </summary> /// <param name="t">进行比较的类型的实例</param>
        /// <param name="t">进行比较的类型的实例</param> /// <param name="name">进行比较对象的属性名称</param>
        /// <param name="name">进行比较对象的属性名称</param> /// <param name="direction">比较方向(升序/降序)</param>
        /// <param name="direction">比较方向(升序/降序)</param> public Reverser(T t, string name, ReverserInfo.Direction direction)
        public Reverser(T t, string name, ReverserInfo.Direction direction) {
        { this.type = t.GetType();
            this.type = t.GetType(); this.info.name = name;
            this.info.name = name; this.info.direction = direction;
            this.info.direction = direction; }
        }
 //必须!实现IComparer<T>的比较方法。
        //必须!实现IComparer<T>的比较方法。 int IComparer<T>.Compare(T t1, T t2)
        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 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);
            object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null); if (this.info.direction != ReverserInfo.Direction.ASC)
            if (this.info.direction != ReverserInfo.Direction.ASC) Swap(ref x, ref y);
                Swap(ref x, ref y); return (new CaseInsensitiveComparer()).Compare(x, y);
            return (new CaseInsensitiveComparer()).Compare(x, y); }
        }
 //交换操作数
        //交换操作数 private void Swap(ref object x, ref object y)
        private void Swap(ref object x, ref object y) {
        { object temp = null;
            object temp = null; temp = x;
            temp = x; x = y;
            x = y; y = temp;
            y = temp; }
        } }
    }
 /// <summary>
    /// <summary> /// 对象比较时使用的信息类
    /// 对象比较时使用的信息类 /// </summary>
    /// </summary> public struct ReverserInfo
    public struct ReverserInfo {
    { /// <summary>
        /// <summary> /// 比较的方向,如下:
        /// 比较的方向,如下: /// ASC:升序
        /// ASC:升序 /// DESC:降序
        /// DESC:降序 /// </summary>
        /// </summary> public enum Direction
        public enum Direction {
        { ASC = 0,
            ASC = 0, DESC,
            DESC, };
        };
 public enum Target
        public enum Target {
        { CUSTOMER = 0,
            CUSTOMER = 0, FORM,
            FORM, FIELD,
            FIELD, SERVER,
            SERVER, };
        };
 public string name;
        public string name; public Direction direction;
        public Direction direction; public Target target;
        public Target target; }
    }
上面主要是运用了 C#的反射 和 Framework中的排序算法。
像上面那样实现接口后,就可以使用List<T>进行 升序/降序 排序了。
测试代码如下:

 using System;
using System; using System.Collections.Generic;
using System.Collections.Generic; using System.Collections;
using System.Collections; using System.Reflection;
using System.Reflection; using System.Text;
using System.Text;
 namespace List_T_SortTest_u_2
namespace List_T_SortTest_u_2 {
{ #region 测试Reverser<T>代码段
    #region 测试Reverser<T>代码段
 /// <summary>
    /// <summary> /// 实体类User,测试用
    /// 实体类User,测试用 /// </summary>
    /// </summary> public class User
    public class User {
    { protected string _name;
        protected string _name; protected int _age;
        protected int _age; protected string _address;
        protected string _address;
 public User(string name, int age, string address)
        public User(string name, int age, string address) {
        { this._name = name;
            this._name = name; this._age = age;
            this._age = age; this._address = address;
            this._address = address; }
        }
 public string Name
        public string Name {
        { get { return _name; }
            get { return _name; } set { _name = value; }
            set { _name = value; } }
        }
 public int Age
        public int Age {
        { get { return _age; }
            get { return _age; } set { _age = value; }
            set { _age = value; } }
        }
 public string Address
        public string Address {
        { get { return _address; }
            get { return _address; } set { _address = value; }
            set { _address = value; } }
        } }
    }
 /// <summary>
    /// <summary> /// 主程序类(启动类),测试用
    /// 主程序类(启动类),测试用 /// </summary>
    /// </summary> class Program
    class Program {
    { static void Main(string[] args)
        static void Main(string[] args) {
        { List<User> userList = new List<User>();
            List<User> userList = new List<User>(); User user;
            User user;
 user = new User("Wang", 21, "ShenYang");
            user = new User("Wang", 21, "ShenYang"); userList.Add(user);
            userList.Add(user); user = new User("Yan", 27, "JinZhou");
            user = new User("Yan", 27, "JinZhou"); userList.Add(user);
            userList.Add(user); user = new User("Liu", 26, "BeiJing");
            user = new User("Liu", 26, "BeiJing"); userList.Add(user);
            userList.Add(user); user = new User("Zhao", 30, "ChaoYang");
            user = new User("Zhao", 30, "ChaoYang"); userList.Add(user);
            userList.Add(user); user = new User("Yang", 27, "FuXin");
            user = new User("Yang", 27, "FuXin"); userList.Add(user);
            userList.Add(user);
 //for (int i = 0; i < ar.Count; i++ )
            //for (int i = 0; i < ar.Count; i++ ) //    ;
            //    ; Console.Write("Name     ");
            Console.Write("Name     "); Console.Write("Age      ");
            Console.Write("Age      "); Console.Write("Address  " + " " + " ");
            Console.Write("Address  " + " " + " "); Console.WriteLine("-----------------------");
            Console.WriteLine("-----------------------"); foreach (User u in userList)
            foreach (User u in userList) {
            { Console.Write(u.Name + "    ");
                Console.Write(u.Name + "    "); Console.Write(u.Age + "    ");
                Console.Write(u.Age + "    "); Console.Write(u.Address + "    " + " ");
                Console.Write(u.Address + "    " + " "); }
            } Console.WriteLine();
            Console.WriteLine();
 Reverser<User> reverser = new Reverser<User>(user.GetType(), "Name", ReverserInfo.Direction.DESC);
            Reverser<User> reverser = new Reverser<User>(user.GetType(), "Name", ReverserInfo.Direction.DESC); userList.Sort(reverser);
            userList.Sort(reverser); Console.WriteLine();
            Console.WriteLine(); foreach (User u in userList)
            foreach (User u in userList) {
            { Console.Write(u.Name + "    ");
                Console.Write(u.Name + "    "); Console.Write(u.Age + "    ");
                Console.Write(u.Age + "    "); Console.Write(u.Address + "    " + " ");
                Console.Write(u.Address + "    " + " "); }
            } Console.WriteLine();
            Console.WriteLine();
 reverser = new Reverser<User>(user.GetType(), "Age", ReverserInfo.Direction.ASC);
            reverser = new Reverser<User>(user.GetType(), "Age", ReverserInfo.Direction.ASC); userList.Sort(reverser);
            userList.Sort(reverser); Console.WriteLine();
            Console.WriteLine(); foreach (User u in userList)
            foreach (User u in userList) {
            { Console.Write(u.Name + "    ");
                Console.Write(u.Name + "    "); Console.Write(u.Age + "    ");
                Console.Write(u.Age + "    "); Console.Write(u.Address + "    " + " ");
                Console.Write(u.Address + "    " + " "); }
            }
 Console.Read();
            Console.Read(); }
        } }
    } #endregion
    #endregion
 }
}
         
     
        作者:酷客多小程序 
        
出处: http://www.cnblogs.com/ywqu
如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 
                    
                 
     
         
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号