http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4015|回复: 0

[Asp.Net] ArrayList排序问题

[复制链接]
发表于 2012-7-8 09:49:29 | 显示全部楼层 |阅读模式
直接上代码

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



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-12-19 05:15

© 2014-2021

快速回复 返回顶部 返回列表