zoukankan      html  css  js  c++  java
  • OrderBy排序和IComparer的使用

    一,OrderBy排序在MDSN中有两种使用方法,如下

    1》第一种方法的使用,就是根据某个字段排序,使用默认的比较器(Comparer<T>.default),如下,由于Dictionary是继承IEnumerable的,所以这里可以使用Dictionary作为排序集合,

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TestDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("B", "2");
                dic.Add("A", "1");
                dic.Add("D", "4");
                dic.Add("C", "3");
                StringBuilder str1 = new StringBuilder();
                var param1 = dic.OrderBy(x => x.Key).ToDictionary(x => x.Key, y => y.Value);
                foreach (string dic1 in param1.Keys)
                {
                    str1.Append(dic1+",");
                }
                Console.WriteLine(str1.ToString());
                Console.ReadKey();
            }
        }
    }
    复制代码

    2》第二种方法的使用,按使用指定的比较器按升序对序列的元素进行排序。如下(使用ASCII值排序的例子介绍)

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace TestDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("B", "2");
                dic.Add("A", "1");
                dic.Add("D", "4");
                dic.Add("C", "3");
                StringBuilder str1 = new StringBuilder();
                var param = dic.OrderBy(x => x.Key, new ComparerTest()).ToDictionary(x => x.Key, y => y.Value);
                foreach (string dic1 in param.Keys)
                {
                    str1.Append(dic1);
                }
                Console.WriteLine(str1.ToString());
                Console.ReadKey();
            }
        }
        public class ComparerTest : IComparer<String>
        {
            public int Compare(String x, String y)
            {
                return string.CompareOrdinal(x, y);
            }
        }
    }
    复制代码

     看下面这句的代码,不知道,大家有没有跟我一样的疑惑?如下

    var param = dic.OrderBy(x => x.Key, new ComparerTest()).ToDictionary(x => x.Key, y => y.Value);

    为什么这句话实例ComparerTest这个类就可以完成比较???

    查询一遍第二个方法的源码,如下图

    1,原来指定的比较器接收的是IComparer<TKey> comparer类型,而ComparerTest是继承 IComparer<TKey>实现的比较方法。

    2,相当于IComparer<String> icomparer = new ComparerTest();(这里是显式转换和隐式,想了解可以看http://www.cnblogs.com/May-day/p/6856457.html),这里实现了IComparer中的Compare方法

    3,最后OrderBy调用比较器,即是IComparer中的Compare,排序

  • 相关阅读:
    数据库主从同步相关问题
    前端使用node.js的http-server开启一个本地服务器
    css中height 100vh的应用场景,动态高度百分比布局,浏览器视区大小单位
    通过浏览器F12开发工具快速获取别的网站前端代码的方法
    vue打包app嵌入h5,区分app进入和android,ios显示不同的下载链接
    vue实现验证码倒计时60秒的具体代码
    vue用hbuilderX打包app嵌入h5方式云打包和遇到的问题
    Cookie写不进去问题深入调查 https Secure Cookie
    vue配置手机通过IP访问电脑开发环境
    区块链名词解析:ICO、IFO、IEO和IMO,分别是什么呢?
  • 原文地址:https://www.cnblogs.com/tsql/p/9098584.html
Copyright © 2011-2022 走看看