天天看點

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)

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。