https://www.cnblogs.com/May-day/p/7490334.html

一,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,排序

OrderBy排序和IComparer的使用的更多相关文章

  1. c# 集合中有数字、字符的Orderby排序

    string[] things= new string[] { "105", "101", "102", "103", ...

  2. 关于angularjs的orderby排序

    包子君又来了,,,angularjs有一个非常强大的功能,那就是排序啦,其实也是算filter的一部分呢,包子刚刚做了一个排序,是按照公司的部门的数字大小排列的 由于后台没传来标识,所以我前台自己截图 ...

  3. Entity Framework 在OrderBy排序中使用字符串

    public static class LinqExtensions { private static PropertyInfo GetPropertyInfo(Type objType, strin ...

  4. JOIN,WHERE判断和ORDERBY排序

    MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽可能小. 如果重复代码只是 ...

  5. 排序陷阱 List.Sort Linq.OrderBy

    部分内容摘自:http://www.th7.cn/Program/net/201511/692766.shtml C#框架里面主要提供了两种排序方式:Array.Sort Linq.Orderby. ...

  6. Linq查询操作之排序操作

    在Linq中排序操作可以按照一个或多个关键字对序列进行排序.其中第一个排序关键字为主要关键字,第二个排序关键字为次要关键字.Linq排序操作共包含以下5个基本的操作. 1.OrderBy操作,根据排序 ...

  7. C# IComparable接口、IComparer接口和CompareTo(Object x)方法、Compare()方法

    在项目中经常会用到字符串比较,但是有时候对字符串的操作比较多,规则各异.比如有的地方我们需要用排序规则,有的地方需要忽略大小写,我们该如何写一个比较容易操作的比较方法呢?重新实现IComparer接口 ...

  8. C#排序比较

    与C#定义了相等性比较规范一样,C#也定义了排序比较规范,以确定一个对象与另一个对象的先后顺序.排序规范如下 IComparable接口(包括IComparable接口和IComparable< ...

  9. ServiceStack.OrmLite 笔记10-group having 分页orderby等

    group having 分页等 var ev = OrmLiteConfig.DialectProvider.SqlExpression(); group的使用 同sql一样,注意group分组的字 ...

随机推荐

  1. python 2.7 pip导入django,将python部署到sublime上

    1.安装python 2.7,并且导入第三方库django 下载python 2.7,然后把python2.7的python.exe的路径和pip的路径添加到系统环境变量的path路径下. win+R ...

  2. Java(日期、随机数、系统工具类)

    Date类 一般用于获取时间 Date date1 = new Date();//获取当前系统时间 Date date2 = new Date(10000);//获取从标准基准时间起10000毫秒的时 ...

  3. 常规DP专题练习

    POJ2279 Mr. Young's Picture Permutations 题意 Language:Default Mr. Young's Picture Permutations Time L ...

  4. python之format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  5. win7和linux下利用命令查看文件md5、sha1、sha256

    win7 certutil -hashfile <filename> MD5 certutil -hashfile <filename> SHA1 certutil -hash ...

  6. mybatis的逆向工程和中文注解

    由于MyBatis Generator自带了生成注释的功能,但是,是英文的而且生成的根本无法理解,所以可以通过,修改他的源码来实现生成中文的注释,具体方式有以下几种: 1) 自定义CommentGen ...

  7. LeetCode——295. Find Median from Data Stream

    一.题目链接: https://leetcode.com/problems/find-median-from-data-stream 二.题目大意: 给定一段数据流,要求求出数据流中的中位数,其中数据 ...

  8. python css选择器

    css 选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  9. python之路——22

    学习内容 1.初识面向对象 类:抽象的,模子 对象:具体的,根据类规范 代码精简,修改方便,属性规范2.对象 查看属性 调用方法 __dict__,增删改查,通过字典语法进行3.类名 1.实例化 2. ...

  10. docker-容器,仓库

    ---恢复内容开始--- 前言: 学技术不能该断时间,连续的学习才是最好的学习方式. 00x1: 创建一个容器:docker create -it xxxx 而启动容器就有两种状态了,第一:新容器启动 ...