加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

.Net中的IOrderedEnumerable.ThenBy()如何工作?

发布时间:2020-12-17 00:10:02 所属栏目:大数据 来源:网络整理
导读:我想了解ThenBy在.Net中的工作原理. (我知道如何使用它,我只是不明白微软如何实现它!) 根据文档,string_list.OrderBy(Function(x)x.length).ThenBy(Function(x)x)应该输出按长度排序的字符串列表,然后按字母顺序排列.怎么可能工作?第一类是长度.第二种排序
我想了解ThenBy在.Net中的工作原理. (我知道如何使用它,我只是不明白微软如何实现它!)

根据文档,string_list.OrderBy(Function(x)x.length).ThenBy(Function(x)x)应该输出按长度排序的字符串列表,然后按字母顺序排列.怎么可能工作?第一类是长度.第二种排序应该撤消第一个排序!

假设这个代码:

Dim sorted_by_length As IOrderedEnumerable(Of String)
sorted_by_length = string_list.OrderBy(Function (x) x.length)
sorted_by_length = sorted_by_length.ThenBy(Function

这是我试图实现最后一行而不使用ThenBy:

Dim sorted_by_length As IOrderedEnumerable(Of String)
sorted_by_length = string_list.OrderBy(Function (x) x.length)
'my implementation of OrderBy:
Dim e as IEnumerator(Of String) = sorted_by_length.GetEnumerator
Do While e.MoveNext
    'I have no idea what to write here!
Loop

这里有一些魔法吗?有没有一些e.GetPreviousKeySelector()函数?其实我甚至不能写一个返回IOrderedEnumerable的函数!

How could it possibly work?!? The first sort is by length. The second sort should undo the sorting of the first one!

不,只有当主比较找到两个相等的值时,才会查询第二种排序比较.

IOrderedEnumerable实现是通过有效地记住所有比较来实现的,或者作为另一种方法,允许您从“当前比较和另一个比较返回0时进行比较”来构建比较.

我有一个blog post series,它进一步深入LINQ to Objects,提供了一个完整的替代实现.在part 26a和26b中涵盖了IOrderedEnumerable的基础,26c和26d的更多细节和优化.

In fact,I can’t even write a function that returns IOrderedEnumerable!

你绝对可以 – 返回从OrderBy返回的值,或者通过自己实现.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读