zoukankan      html  css  js  c++  java
  • 使 SortList 实现重复键排序

    SortList 默认对按Key来排序,且Key值不能重复,但有时可能需要用有重复值的Key来排序,以下是实现方式:

    1、对强类型:以float为例

     #region 使SortList能对重复键排序

        internal class ListComparer : IComparer<float>
        {
            static private ListComparer mono;
            public static ListComparer EarlyFirst
            {
                get
                {
                    if (mono == null)
                        mono = new ListComparer();
                    return mono;
                }
            }


            #region IComparer 成员
            public int Compare(float x, float y)
            {
                if (x == y)
                    return -1;
                else if (x < y)
                    return -1;
                else
                    return 1;
            }
            #endregion

        }

        internal class CList : SortedList<float, int>
        {
            public CList()
                :base(ListComparer.EarlyFirst)
            {
            }
        }
        #endregion

     用法:直接用CList类代替SortedList类

    2、对非强类型:

      首先要实现IComparer接口

       internal class ListComparer : IComparer
        {
            #region IComparer 成员
            public int Compare(object x, object y)
            {
               //return -1;//不排序

                //排序
                 int iResult = (int)x - (int)y;
                if(iResult == 0) iResult = -1;
                return iResult;       
            }
            #endregion

        }

     用法:

       SortList  st=new SortList(new ListComparer());

       st.add(11,23);

       st.add(22,23);

       st.add(11,23);

      输出结果是11,11,22

  • 相关阅读:
    XCode5中新建工程后强制使用了ARC,如何去掉?
    面向对象程序的设计原则--Head First 设计模式笔记
    ios控件自定义指引
    iOS UITableViewDelegate && UITableViewDataSource 执行顺序
    awakeFromNib方法和viewDidLoad方法区别
    ios 视图的旋转及应用
    线段树模板 (刘汝佳)
    poj 3468
    hdu 2829(四边形优化 && 枚举最后一个放炸弹的地方)
    poj 3517(约瑟夫环问题)
  • 原文地址:https://www.cnblogs.com/coolsundy/p/3821854.html
Copyright © 2011-2022 走看看