如何在使用.NET确实IOrderedEnumerable.ThenBy()?确实、如何在、ThenBy、NET

2023-09-03 15:51:48 作者:我走路带风

我想了解ThenBy是如何工作的.NET。 (我知道如何使用它,我只是不明白怎么微软实现了它!)

根据该文件, string_list.OrderBy(功能(X)x.length).ThenBy(功能(x)x)应输出字符串命令者名单长度的然后的字母顺序排列。怎么可能可能工作?!?第一类是按长度。第二类应该撤销第一位的排序!

假设这code:

 昏暗sorted_by_length作为IOrderedEnumerable(串)
sorted_by_length = string_list.OrderBy(功能(X)x.length)
sorted_by_length = sorted_by_length.ThenBy(功能
 

下面是我想要实现的最后一行,而无需使用 ThenBy

 昏暗sorted_by_length作为IOrderedEnumerable(串)
sorted_by_length = string_list.OrderBy(功能(X)x.length)
'我实现排序依据的:
昏暗的E上的IEnumerator(字符串)= sorted_by_length.GetEnumerator
做,而e.MoveNext
    我不知道在这里写的是什么!
循环
 

有一些神奇的事情在这里......是有一些e.Get previousKeySelector()函数?事实上,我甚至不能写一个函数,返回IOrderedEnumerable!

解决方案   

怎么可能可能工作?!?第一类是按长度。第二类应该撤销第一位的排序!

电脑直连网线,ipv4显示无internet访问权限

没有,第二个排序比较,只有当主比较发现两个相等值的查询。

IOrderedEnumerable 实施记住所有的比较,有效地做到这一点 - 或者,如把它,让你打造从当前比较的比较另一种方式另外一个来咨询的时候返回0。

我有一个博客文章系列其进入的LINQ to对象在一定的深度,提供了一个完整的备选方案的实施。 IOrderedEnumerable 是覆盖在part 26A 和26b,更多的细节和优化26c和26d.

  

事实上,我甚至不能写一个函数,返回IOrderedEnumerable!

您绝对可以 - 无论是通过返回从排序依据返回的值,或自行实施它

I want to understand how ThenBy works in .Net. (I know how to use it, I just don't understand how Microsoft implemented it!)

According to the documentation, string_list.OrderBy(Function (x) x.length).ThenBy(Function (x) x) should output a list of strings ordered by length and then alphabetically. How could it possibly work?!? The first sort is by length. The second sort should undo the sorting of the first one!

Assume this code:

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

Here's me trying to implement the last line without using 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

There's some magic going on here... Is there some e.GetPreviousKeySelector() function? In fact, I can't even write a function that returns IOrderedEnumerable!

解决方案

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

No, the second sort comparison is only consulted when the primary comparison finds two equal values.

The IOrderedEnumerable implementation does this by remembering all the comparisons, effectively - or, as another way of putting it, allowing you to build a comparison from "the current comparison and another one to consult when that returns 0".

I have a blog post series which goes into LINQ to Objects in some depth, providing a complete alternative implementation. The basis of IOrderedEnumerable is covered in part 26a and 26b, with more details and optimization in 26c and 26d.

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

You absolutely can - either by returning the value returned from OrderBy, or by implementing it yourself.