【问题标题】:Order two dictionaries based on the keys根据键对两个字典进行排序
【发布时间】:2019-07-30 10:43:04
【问题描述】:

我有两个具有相同键的字典。 第一个包含特定键的计数,第二个类似于查找,它包含特定键的真实“值”。

这是一个例子:

1.字典:

Key:    Value:

0;       10
1;       17
2;       3
3;       28
4;       8

2。字典:

Key:    Value:

0;       String1
1;       String2
2;       String3
3;       String4
4;       String5

现在我需要按照 1. 字典的值对这些字典进行排序。我只知道如何订购第一本,但我不知道如何订购第二本字典。

预期的输出是:

1.字典:

Key:    Value:

0;       28
1;       17
2;       10
3;       8
4;       3

2。字典:

Key:    Value:

0;       String4
1;       String2
2;       String1
3;       String5
4;       String3

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 字典没有(保证的)顺序,您永远不应该依赖字典来任何特定的顺序。
  • 如果您不提供所有相关信息,您希望别人如何帮助您?
  • 对字典进行排序的想法对我来说似乎很奇怪。您可能想要dict 2 中按dict 1 中的数字排序的元素列表吗?
  • 您的密钥似乎没有任何用途,因为您无论如何都在更改它们。那么,为什么不重新考虑您的设计,将实际值和它们的数字一起分配,而不是同步两个字典呢?

标签: c# .net list linq dictionary


【解决方案1】:

好吧,字典(像这些)

var dict1 = new Dictionary<int, int>() {
  {0, 10},
  {1, 17},
  {2,  3},
  {3, 28},
  {4,  8}, 
};

var dict2 = new Dictionary<int, string>() {
  {0, "String 1"},
  {1, "String 2"},
  {2, "String 3"},
  {3, "String 4"},
  {4, "String 5"}, 
};

没有任何订单。但是,我们可以表示(在 Linq 的帮助下)排序的数据:

  // Just an OrderBy...
  var result1 = dict1
    .OrderByDescending(pair => pair.Value);

  // It may appear, that dict1 doesn't have corresponding value
  // that's why we have to introduce "found" variable
  var result2 = dict2
    .Select(pair => {
      bool found = dict1.TryGetValue(pair.Key, out var value);

      return new {
        pair,
        found,
        value
      };
    })
    .OrderByDescending(item => item.found)
    .ThenByDescending(item => item.value)
    .Select(item => item.pair);

 Console.WriteLine(string.Join(Environment.NewLine, result1); 
 Console.WriteLine();
 Console.WriteLine(string.Join(Environment.NewLine, result2); 

结果:

[3, 28]
[1, 17]
[0, 10]
[4, 8]
[2, 3]

[3, String 4]
[1, String 2]
[0, String 1]
[4, String 5]
[2, String 3]

如果您只想枚举Values(没有Keys),我们可以再添加一个Select

.Select((pair, i) => $"{i + 1}; {pair.Value}");

像这样:

var result1 = dict1
    .OrderByDescending(pair => pair.Value)
    .Select((pair, i) => $"{i + 1}; {pair.Value}");

var result2 = dict2
    .Select(pair => {
      bool found = dict1.TryGetValue(pair.Key, out var value);

      return new {
        pair,
        found,
        value
      };
    })
    .OrderByDescending(item => item.found)
    .ThenByDescending(item => item.value)
    .Select(item => item.pair)
    .Select((pair, i) => $"{i + 1}; {pair.Value}");

结果:

1; 28
2; 17
3; 10
4; 8
5; 3

1; String 4
2; String 2
3; String 1
4; String 5
5; String 3

【讨论】:

    【解决方案2】:

    如果key 对于具有相同序列的两个字典来说是相同的,那么这就是设计。
    将两个字典合二为一并排序,然后在其上生成序列号。

    var dict1 = new Dictionary<int, String>();
    dict1.Add(10, "string1");
    dict1.Add(17, "string2");
    dict1.Add(3, "string3");
    dict1.Add(28, "string4");
    dict1.Add(8, "string5");
    
    var dict2 = new Dictionary<int, String>();
    int i = 0;
    foreach (KeyValuePair<int, string> author in dict1.OrderByDescending(key => key.Key))
    {
        dict2.Add(i, author.Value);
        i++;
    }
    
    foreach (KeyValuePair<int, string> author in dict2)
    {
        Console.WriteLine("Key: {0}, Value: {1}", author.Key, author.Value);
    }
    

    DEMO

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多