【问题标题】:Sorting an IOrderedEnumerable into an arbitrary order将 IOrderedEnumerable 排序为任意顺序
【发布时间】:2017-01-10 12:23:10
【问题描述】:

我有一个IOrderedEnumerable<IGrouping<string,var>>IGrouping 中的值与问题无关,因此用 var 抽象)。我有一个List<string>,其中包含用作IGrouping 的键的字符串。我希望能够按键重新排序IOrderedEnumerable,以便IGroupings 与List 保持相同的顺序。

我查看了IOrderedEnumerable.OrderBy(v => v.Key),但这似乎只是按升序排列 - 我看不出有什么方法可以将其用作参考。我认为诀窍是编写一个可以做到这一点的键选择器,但我的 LINQ-fu 不够强大。

【问题讨论】:

  • 这是否意味着 ThenBy 仅更改 IOrderedEnumerable 中的相同顺序元素,或者它与 IOrderedEnumerable 无关?

标签: c# linq


【解决方案1】:
iOrderedEnumerable.OrderBy(v => thatListOfString.IndexOf(v.Key))

如果您想避免在列表中再次搜索每个v,您可以cache the positions in a dictionary

var cached_positions = thatListOfString
                          .Select((value, index) => new { value, index })
                          .ToDictionary(pair => pair.value, pair => pair.index);

iOrderedEnumerable.OrderBy(v => cached_positions[v.Key])

【讨论】:

  • 这对于大量数据来说似乎效率很低。
  • @M.kazemAkhgary 过早优化? :) 另一方面,源代码可以有 10 个条目,并且代码将每周执行一次。虽然我总是可以接受更好的解决方案(如果有的话)。也投票给他们
【解决方案2】:

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication34
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            List<string> orderStrings = new List<string>() { "b", "a", "z" };

            List<List<object>> data = new List<List<object>>() {
                new List<object> {"a", 1},
                new List<object> {"a", 2},
                new List<object> {"a", 3},
                new List<object> {"a", 4},
                new List<object> {"b", 5},
                new List<object> {"b", 6},
                new List<object> {"b", 7},
                new List<object> {"z", 8},
                new List<object> {"z", 9},
            };

            var results = data.GroupBy(x => x[0]).OrderBy(x => orderStrings.IndexOf((string)x.Key)).ToList();

        }


    }


}

【讨论】:

  • 这似乎没有为现有答案添加任何内容。
  • 您的答案使用字典,而我的没有。我的解决方案也比你的代码简单得多。
  • 你的回答是我回答的第一行。然后在我的回答中,我还展示了如何使用字典来提高速度。
  • 发帖的人会知道吗?我的代码运行并给出了真实的答案,你的呢?
  • 我确实发现(在盯着它一两分钟后)这个答案的大部分与测试用例的设置无关,并且实际答案是与已经提供了答案。
【解决方案3】:

怎么样:

thatListOfString
  .Select((orderIndex, stringValue)=>new{orderIndex, stringValue})
  .Join(
    orderedGroups,
    left=>left.stringValue,
    right=>right.Key,
    (left,right)=>new{left,right}
  )
  .OrderBy(lr=>lr.left.orderIndex)//actually with current internal implementation of Enumerable.Join this one is not needed
  .Select(lr=>lr.right);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-29
    • 2011-08-03
    • 2012-01-04
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2011-01-13
    相关资源
    最近更新 更多