想对ArrayList排一排序,却发现还要写一个继承自IComparer的类,用其它方法都不行。看来只得写了。呵呵。



    public class NormalDepartmentUserObjectCollection:System.Collections.ArrayList
    {
        bool cvt = true;
        public NormalDepartmentUserObjectCollection()
        {

        }

        public NormalDepartmentUserObjectCollection(System.Collections.ArrayList C)
        {

        }

        public override void Sort()
        {
            cvt = !cvt;
            Sort(new Sorter(cvt));
        }

        class Sorter:System.Collections.IComparer
        {
            bool convert = false;

            internal Sorter(bool cvt)
            {
                convert = cvt;
            }

            #region IComparer 成员

            public int Compare(object x, object y)
            {
                if(convert)
                    return String.Compare(((NormalDepartmentUserObject)y).Name, ((NormalDepartmentUserObject)x).Name);
                else
                    return String.Compare(((NormalDepartmentUserObject)x).Name, ((NormalDepartmentUserObject)y).Name);
            }

            #endregion
        }

        }


很简单,只要实现一个Compare的方法即可,因为我们是对一个自定义类的Name属性进行比较,所以就使用了String的Compare方法。


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
  • 2021-09-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2022-01-18
相关资源
相似解决方案