苏飞论坛

标题: ArrayList排序问题 [打印本页]

作者: snyg    时间: 2012-7-8 09:49
标题: ArrayList排序问题
直接上代码

[C#] 纯文本查看 复制代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Reflection;

    public class SortabledArrayList : ArrayList, IBindingList, IComparer
    {
        private bool isSortedCore = true;
        private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
        private PropertyDescriptor sortPropertyCore = null;
        private string defaultSortItem;
        private ListChangedEventArgs resetEvent = new ListChangedEventArgs(ListChangedType.Reset, -1);

        public SortabledArrayList()
        { }

        public SortabledArrayList(ArrayList list)
            : base(list)
        { }

        #region IBindingList Members

        public event ListChangedEventHandler ListChanged;

        public void RemoveIndex(PropertyDescriptor property)
        {
            throw new NotImplementedException();
        }

        public void AddIndex(PropertyDescriptor property)
        {
            throw new NotImplementedException();
        }

        public object AddNew()
        {
            throw new NotImplementedException();
        }

        public bool AllowEdit
        {
            get { return true; }
        }

        public bool AllowNew
        {
            get { return false; }
        }

        public bool AllowRemove
        {
            get { return true; }
        }

        public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
        {
            isSortedCore = true;
            sortPropertyCore = property;
            sortDirectionCore = direction;
            SortCore();
        }

        public int Find(PropertyDescriptor property, object key)
        {
            for (int i = 0; i < this.Count; i++)
            {
                if (Equals(property.GetValue(this), key)) return i;
            }
            return -1;
        }

        public bool IsSorted
        {
            get { return true; }
        }

        public void RemoveSort()
        {
            if (isSortedCore)
            {
                isSortedCore = false;
                sortPropertyCore = null;
                sortDirectionCore = ListSortDirection.Ascending;
                SortCore();
            }
        }

        public ListSortDirection SortDirection
        {
            get { return sortDirectionCore; }
        }

        public PropertyDescriptor SortProperty
        {
            get { return sortPropertyCore; }
        }

        public bool SupportsChangeNotification
        {
            get { return true; }
        }

        public bool SupportsSearching
        {
            get { return true; }
        }

        public bool SupportsSorting
        {
            get { return true; }
        }

        #endregion

        public string DefaultSortItem
        {
            get { return defaultSortItem; }
            set
            {
                if (defaultSortItem != value)
                {
                    defaultSortItem = value;
                    SortCore();
                }
            }
        }

        private void SortCore()
        {
            ArrayList list = this as ArrayList;
            list.Sort(this);
            ResetArrayList();
        }

        protected virtual void ResetArrayList()
        {
            if (ListChanged != null)
                ListChanged(this, resetEvent);
        }

        private static int CompareValue(object o1, object o2, Type type)
        {
            //这里改成自己定义的比较
            if (o1 == null)
                return (o2 == null ? 0 : -1);
            else if (o2 == null)
                return 1;
            else if (type.IsPrimitive || type.IsEnum)
                return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
            else if (type == typeof(DateTime))
                return Convert.ToDateTime(o1).CompareTo(o2);
            else
                return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
        }

        #region IComparer Members

        public int Compare(object x, object y)
        {
            int ret = 0;
            if (SortProperty != null)
            {
                ret = CompareValue(SortProperty.GetValue(x), SortProperty.GetValue(y), SortProperty.PropertyType);
            }
            if (ret == 0 && DefaultSortItem != null)
            {
                if (x == null || y == null)
                {
                    ret = CompareValue(x, y, typeof(object));
                }
                else
                {
                    PropertyInfo property = x.GetType().GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], null);
                    if (property != null)
                    {
                        ret = CompareValue(property.GetValue(x, null), property.GetValue(y, null), property.PropertyType);
                    }
                }
            }
            if (SortDirection == ListSortDirection.Descending) ret = -ret;
            return ret;
        }

        #endregion
    }






欢迎光临 苏飞论坛 (http://www.sufeinet.com/) Powered by Discuz! X3.4