现在的位置: 首页 > 综合 > 正文

List排序

2013年10月21日 ⁄ 综合 ⁄ 共 3364字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Collections;

namespace TestOne
{

    public delegate void GreetingDelete(string name);
    class Program
    {
        static void Main(string[] args)
        {

            List<User> userList = new List<User>();
            userList.Add(new User("Wang", 21, "ShenYang"));
            userList.Add(new User("Zhang", 22, "HeNan"));
            userList.Add(new User("ss", 25, "TianJin"));
            userList.Add(new User("刘", 19, "BeiJin"));
            userList.Add(new User("Liang", 23, "ShangHai"));
            userList.Add(new User("01", 25, "TianJin"));
            userList.Add(new User("09", 25, "TianJin"));
            userList.Add(new User("03", 25, "TianJin"));
            userList.Add(new User("12", 25, "TianJin"));
            userList.Add(new User("Z1", 25, "TianJin"));
            userList.Add(new User("谌", 25, "TianJin"));
            userList.Add(new User("98", 25, "TianJin"));
            userList.Add(new User("王", 25, "TianJin"));

            User user;
            user=new User("Liu",30,"NanJing");
            userList.Add(user);

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

            foreach (User u in userList)
                Console.WriteLine(u.Name + "\n");


            Console.ReadLine();
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;

namespace TestOne
{
    class Reverser<T>:IComparer<T>
    {
        private Type type = null;
        private ReverserInfo info;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="type">进行比较的类类型</param>
        /// <param name="name">进行比较的对象的属性名称</param>
        /// <param name="direction">比较方向</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 name="classname">进行比较的类名称</param>
        /// <param name="name">进行比较的属性名称</param>
        /// <param name="direction">比较方向</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 name="t">比较的类的实例</param>
        /// <param name="name">比较对象的属性名称</param>
        /// <param name="direction">比较方向</param>
        public Reverser(T t, string name, ReverserInfo.Direction direction)
        {
            this.type = t.GetType();
            this.info.name = name;
            this.info.direction = direction;
        }

        /// <summary>
        /// 比较的方法
        /// </summary>
        /// <param name="t1"></param>
        /// <param name="t2"></param>
        /// <returns></returns>
        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;
        }
    }


    public struct ReverserInfo 
    {
        public enum Direction
        {
            ASC,
            DESC
        };

        public enum Target
        {
            CUSTOMER=0,
            FORM,
            FIELD,
            SERVER
        };

        public string name;
        public Direction direction;
        public Target target;
    }

    public class User
    {
        private string _name;
        protected int _age;
        protected string _address;

        public User(string name, int age, string address)
        {
            this._name = name;
            this._address = address;
            this._age = age;
        }

        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; }
        }
    }
}



抱歉!评论已关闭.