如何在 LINQified Regex 中使用 OrderBy<>?

How can I use OrderBy<> in my LINQified Regex?

我在这里找到了如何从 html 文件中提取不同的元素

...结果很好。我得到(截断):

<span class="sam" title="This is Sam Clemens speaking">
<span class="others" title="This is 'The Sphynx' speaking">
<span class="others" title="This is the stagecoach driver speaking">
<span class="others" title="This is someone other than the main characters speaking">
<span class="others" title="The station-boss is speaking">
<span class="others" title="This is 'an elderly pilgrim' speaking">
<span class="others2" title="This is Jack speaking">
<span class="bemis" title="This is Bemis speaking">

不过我想对这些进行排序,所以上面的是:

<span class="bemis" title="This is Bemis speaking">
<span class="sam" title="This is Sam Clemens speaking">
<span class="others" title="The station-boss is speaking">
<span class="others" title="This is 'an elderly pilgrim' speaking">
<span class="others" title="This is 'The Sphynx' speaking">
<span class="others" title="This is someone other than the main characters speaking">
<span class="others" title="This is the stagecoach driver speaking">
<span class="others2" title="This is Jack speaking">

这行代码:

distinct_values = MyRegex.Matches(str).Cast<Match>().Select(p => p.Value).Distinct().ToList();

...获取数据如前所示。我想我需要做这样的事情:

distinct_values = MyRegex.Matches(str).Cast<Match>().Select(p => p.Value).Distinct().OrderBy<>

...但即使我是对的,我也不知道 OrderBy 的尖括号内应该包含什么。

与Select相同:

 .OrderBy(p => p.PropertyName)

其中 PropteryName 是您要订购的 属性 的名称。

如果您希望 return 值作为列表,则:

  .OrderBy(p => p.PropertyName).ToList();

在你的例子中,因为它不是一个对象而是一个字符串:

  .OrderBy(p => p).ToList();

尖括号?没什么,这将被推断。重要的是论点:

.OrderBy(p => p).ToList();

当然,它只按预期工作,因为这里按整个字符串排序就足够了。如果没有,您可能想要 select 实际字符串和另一个只有例如class 值。