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

  • 相关阅读:
    在react中实现CSS模块化
    react 组件的生命周期
    HTTP缓存机制与原理
    H5新增API和操作DOM
    js操作json方法总结
    gulp详细教程——前端自动化构建工具
    JavaScript你必须掌握的8大知识点
    HTTP请求与服务器响应流程
    max-height实现任意高度元素的展开收缩动画
    移动端轮播图手势分析+源码
  • 原文地址:https://www.cnblogs.com/coolsundy/p/3821854.html
Copyright © 2011-2022 走看看