天天看点

C#中List的排序(Sort)

要对自定义类数组或List进行排序,譬如:

List<User> userList;

ArrayList arrayList;

最重要的是:继承IComparer<T>接口,实现int IComparer<T>.Compare(T t1, T t2)方法。

代码如下:

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

    /**//// <summary>

C#中List的排序(Sort)

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

C#中List的排序(Sort)

    /// </summary>

C#中List的排序(Sort)

    /// <typeparam name="T">T为泛用类型</typeparam>

C#中List的排序(Sort)

    public class Reverser<T> : IComparer<T>

C#中List的排序(Sort)
C#中List的排序(Sort)

    ...{

C#中List的排序(Sort)

        private Type type = null;

C#中List的排序(Sort)

        private ReverserInfo info;

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        /**//// <summary>

C#中List的排序(Sort)

        /// 构造函数

C#中List的排序(Sort)

        /// </summary>

C#中List的排序(Sort)

        /// <param name="type">进行比较的类类型</param>

C#中List的排序(Sort)

        /// <param name="name">进行比较对象的属性名称</param>

C#中List的排序(Sort)

        /// <param name="direction">比较方向(升序/降序)</param>

C#中List的排序(Sort)

        public Reverser(Type type, string name, ReverserInfo.Direction direction)

C#中List的排序(Sort)
C#中List的排序(Sort)

        ...{

C#中List的排序(Sort)

            this.type = type;

C#中List的排序(Sort)

            this.info.name = name;

C#中List的排序(Sort)

            if (direction != ReverserInfo.Direction.ASC)

C#中List的排序(Sort)

                this.info.direction = direction;

C#中List的排序(Sort)

        }

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        /// <param name="className">进行比较的类名称</param>

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        public Reverser(string className, string name, ReverserInfo.Direction direction) ...{

C#中List的排序(Sort)

            try

C#中List的排序(Sort)
C#中List的排序(Sort)

            ...{

C#中List的排序(Sort)

                this.type = Type.GetType(className, true);

C#中List的排序(Sort)

                this.info.name = name;

C#中List的排序(Sort)
C#中List的排序(Sort)

            }

C#中List的排序(Sort)
C#中List的排序(Sort)

            catch (Exception e)...{

C#中List的排序(Sort)

                throw new Exception(e.Message);

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        /// <param name="t">进行比较的类型的实例</param>

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

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

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            this.type = t.GetType();

C#中List的排序(Sort)
C#中List的排序(Sort)

            this.info.direction = direction;

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        //必须!实现IComparer<T>的比较方法。

C#中List的排序(Sort)

        int IComparer<T>.Compare(T t1, T t2)

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            object x = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t1, null);

C#中List的排序(Sort)

            object y = this.type.InvokeMember(this.info.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty, null, t2, null);

C#中List的排序(Sort)

            if (this.info.direction != ReverserInfo.Direction.ASC)

C#中List的排序(Sort)

                Swap(ref x, ref y);

C#中List的排序(Sort)

            return (new CaseInsensitiveComparer()).Compare(x, y);

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        //交换操作数

C#中List的排序(Sort)

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

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            object temp = null;

C#中List的排序(Sort)

            temp = x;

C#中List的排序(Sort)

            x = y;

C#中List的排序(Sort)

            y = temp;

C#中List的排序(Sort)
C#中List的排序(Sort)

    }

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

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

C#中List的排序(Sort)
C#中List的排序(Sort)

    public struct ReverserInfo

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        /// 比较的方向,如下:

C#中List的排序(Sort)

        /// ASC:升序

C#中List的排序(Sort)

        /// DESC:降序

C#中List的排序(Sort)
C#中List的排序(Sort)

        public enum Direction

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            ASC = 0,

C#中List的排序(Sort)

            DESC,

C#中List的排序(Sort)

        };

C#中List的排序(Sort)
C#中List的排序(Sort)

        public enum Target

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            CUSTOMER = 0,

C#中List的排序(Sort)

            FORM,

C#中List的排序(Sort)

            FIELD,

C#中List的排序(Sort)

            SERVER,

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        public string name;

C#中List的排序(Sort)

        public Direction direction;

C#中List的排序(Sort)

        public Target target;

C#中List的排序(Sort)
C#中List的排序(Sort)

上面主要是运用了 C#的反射 和 Framework中的排序算法。

像上面那样实现接口后,就可以使用List<T>进行 升序/降序 排序了。

测试代码如下:

C#中List的排序(Sort)
C#中List的排序(Sort)

using System;

C#中List的排序(Sort)

using System.Collections.Generic;

C#中List的排序(Sort)

using System.Collections;

C#中List的排序(Sort)

using System.Reflection;

C#中List的排序(Sort)

using System.Text;

C#中List的排序(Sort)
C#中List的排序(Sort)

namespace List_T_SortTest_u_2

C#中List的排序(Sort)
C#中List的排序(Sort)

...{

C#中List的排序(Sort)
C#中List的排序(Sort)

    测试Reverser代码段#region 测试Reverser<T>代码段

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

    /// 实体类User,测试用

C#中List的排序(Sort)
C#中List的排序(Sort)

    public class User

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        protected string _name;

C#中List的排序(Sort)

        protected int _age;

C#中List的排序(Sort)

        protected string _address;

C#中List的排序(Sort)
C#中List的排序(Sort)

        public User(string name, int age, string address)

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            this._name = name;

C#中List的排序(Sort)

            this._age = age;

C#中List的排序(Sort)

            this._address = address;

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        public string Name

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            get ...{ return _name; }

C#中List的排序(Sort)
C#中List的排序(Sort)

            set ...{ _name = value; }

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        public int Age

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            get ...{ return _age; }

C#中List的排序(Sort)
C#中List的排序(Sort)

            set ...{ _age = value; }

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        public string Address

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            get ...{ return _address; }

C#中List的排序(Sort)
C#中List的排序(Sort)

            set ...{ _address = value; }

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

    /// 主程序类(启动类),测试用

C#中List的排序(Sort)
C#中List的排序(Sort)

    class Program

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

        static void Main(string[] args)

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            List<User> userList = new List<User>();

C#中List的排序(Sort)

            User user;

C#中List的排序(Sort)
C#中List的排序(Sort)

            user = new User("Wang", 21, "ShenYang");

C#中List的排序(Sort)

            userList.Add(user);

C#中List的排序(Sort)

            user = new User("Yan", 27, "JinZhou");

C#中List的排序(Sort)
C#中List的排序(Sort)

            user = new User("Liu", 26, "BeiJing");

C#中List的排序(Sort)
C#中List的排序(Sort)

            user = new User("Zhao", 30, "ChaoYang");

C#中List的排序(Sort)
C#中List的排序(Sort)

            user = new User("Yang", 27, "FuXin");

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            //for (int i = 0; i < ar.Count; i++ )

C#中List的排序(Sort)

            //    ;

C#中List的排序(Sort)

            Console.Write("Name     ");

C#中List的排序(Sort)

            Console.Write("Age      ");

C#中List的排序(Sort)

            Console.Write("Address  " + " " + " ");

C#中List的排序(Sort)

            Console.WriteLine("-----------------------");

C#中List的排序(Sort)

            foreach (User u in userList)

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

                Console.Write(u.Name + "    ");

C#中List的排序(Sort)

                Console.Write(u.Age + "    ");

C#中List的排序(Sort)

                Console.Write(u.Address + "    " + " ");

C#中List的排序(Sort)
C#中List的排序(Sort)

            Console.WriteLine();

C#中List的排序(Sort)
C#中List的排序(Sort)

            Reverser<User> reverser = new Reverser<User>(user.GetType(), "Name", ReverserInfo.Direction.DESC);

C#中List的排序(Sort)

            userList.Sort(reverser);

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            reverser = new Reverser<User>(user.GetType(), "Age", ReverserInfo.Direction.ASC);

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

            Console.Read();

C#中List的排序(Sort)
C#中List的排序(Sort)
C#中List的排序(Sort)

    #endregion

C#中List的排序(Sort)
C#中List的排序(Sort)

}

C#中List的排序(Sort)

版权

作者:灵动生活 郝宪玮

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

C#中List的排序(Sort)

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。