【问题标题】:How can I order a List of Dictionary<int, string> based on Dictionary int value?如何根据 Dictionary int 值订购 Dictionary<int, string> 列表?
【发布时间】:2011-12-28 11:07:09
【问题描述】:

我有这份清单:

List<Dictionary<int, string>> list = new List<Dictionary<int, string>>();

我想根据 Dictionary int 值对其进行排序(升序)。我该怎么做?

例子:

Dictionary<int, string> Min1 = new Dictionary<int, string>();
Dictionary<int, string> Min2 = new Dictionary<int, string>();
Dictionary<int, string> Min3 = new Dictionary<int, string>();
Dictionary<int, string> Min4 = new Dictionary<int, string>();
Dictionary<int, string> Min5 = new Dictionary<int, string>();

Min1.Add(3, "name");
Min2.Add(1, "hello");
Min3.Add(5, "marco");
Min4.Add(4, "is");
Min5.Add(2, "my");

List<Dictionary<int, string>> list = new List<Dictionary<int, string>>();
list.Add(Min1);
list.Add(Min2);
list.Add(Min3);
list.Add(Min4);
list.Add(Min5);

// NOW I NEED TO ORDER list BASED ON DICTIONARY INT VALUE
// SO THE ORDER ON THE LIST SHOULD BE Min2, Min5, Min1, Min4, Min3

我想对列表进行排序,所以当我循环它并到达字符串时,我会打印“hello my name is marco”

【问题讨论】:

  • 你能举个你想要的排序例子吗?
  • 这没有多大意义。你想按每个字典中最小的键排序吗?你必须有一个值来比较不同的字典,并且需要选择它是哪一个。
  • 你可以使用 SortedList 类
  • 您的示例没有显示您的字典和列表之间的关系,您在示例中所说的字典只是键值对而不是字典。
  • 仍然你的字典不好至少有一本大小为 2 的字典并显示你想要的,事实上在当前示例中你只需要 KeyValue 对而不是字典,事实上它们都可以在一个字典。

标签: c# linq list dictionary


【解决方案1】:

如果你想展平数据并对其进行排序,你可以试试这个:

list.SelectMany(x=>x).OrderBy(x=>x.Key)

但如果您不喜欢展平数据而只对字典进行排序,您可以这样做:

list.Select(x=>x.OrderBy(y=>y.Key));

编辑:据我了解,您只需要使用一本字典,因此您可以这样做:

Dictionary<int,string> dic = new Dictionary<int,string>();
var result = dic.OrderBy(x=>x.Key).ToLookup(x=>x);

如果您想使用 List 而不是字典(在您的情况下似乎不好),您可以这样做;

 List<KeyValuePair<int,string>> lst1 = new List<KeyValuePair<int,string>>();
 var result = lst1.OrderBy(x=>x.Key).ToLookup(x=>x);

【讨论】:

    【解决方案2】:

    您可以在定义泛型时使用 SortedDictionary 或 SortedList。 当您这样做时,无论您以什么顺序将元素添加到字典中,元素都将以基于键元素的排序格式存储(在您的情况下是 int 类型)。 此外,SortedDictionary 使用 BinarySearch 树,这使其更省时。

    【讨论】:

    • 还有一件事,我想知道你为什么使用 5 个字典,而单个字典 (min1) 可以存储你所有的 KeyValue Pairs。
    【解决方案3】:

    一旦你有了一个字典,你就可以使用 LINQ 根据字典中的键创建一个有序的值列表,如下所示:

    var dict = new List<Dictionary<int, string>>();
    var list = dict.OrderBy(e => e.Key).ToList();
    

    如果没有 LINQ,您可以使用 SortedList:

    SortedList<int, string> sortList = new SortedList<int, string>(dict);
    

    从这里,如果你想要一个普通的List&lt;T&gt; 那么:

    List<string> list = new List<string>(sortList.Values);
    

    【讨论】:

      【解决方案4】:

      但是,Marco,我将上述场景视为一个挑战,并对您想要的结果进行了代码实现。我在下面发布代码。简而言之,您必须创建另一个列表来存储排序的字典元素。您可以在调试器中运行以下代码并自行查看结果。

       public class program
      {
      
          public static void Main()
          {
      
        SortedDictionary<int, string> Min1 = new SortedDictionary<int, string>();
        SortedDictionary<int, string> Min2 = new SortedDictionary<int, string>();
        SortedDictionary<int, string> Min3 = new SortedDictionary<int, string>();
        SortedDictionary<int, string> Min4 = new SortedDictionary<int, string>();
        SortedDictionary<int, string> Min5 = new SortedDictionary<int, string>();
            Min1.Add(3, "name");
            Min2.Add(1, "hello");
            Min3.Add(5, "marco");
            Min4.Add(4, "is");
            Min5.Add(2, "my");
      
       List<SortedDictionary<int, string>> list = new List<SortedDictionary<int, string>>();
              list.Add(Min1);
              list.Add(Min2);
              list.Add(Min3);
              list.Add(Min4);
              list.Add(Min5);
      
              List<SortedDictionary<int, string>> final_list = new List<SortedDictionary<int, string>>();
              List<int> index = new List<int>() ;
      
              foreach (var element in list)
              {
                  foreach (var elements in element)
                  {
                      index.Add(elements.Key);
                      Console.Write(" " +elements.Value+ " ");
                  }
              }
              index.Sort();
      
      
              foreach (var indexelement in index)
              {
                  foreach (var element in list)
                  {
                      foreach (var elements in element)
                      {
                          if (indexelement == elements.Key)
                          {
                              final_list.Add(element);
                          }
      
                      }
      
                  }
              }
              Console.WriteLine();
              foreach (var element in final_list)
              {
                  foreach (var elements in element)
                  {
                      Console.Write(" " + elements.Value+ " ");
                  }
              }
      
              Console.ReadLine();
          }
      }
      

      编码愉快:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        • 1970-01-01
        • 2023-03-15
        • 2018-01-25
        • 2011-03-05
        相关资源
        最近更新 更多