如何根据Dictionary int值对Dictionary<int, string>的列表进行排序?

3

I have this list :

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

我希望能按照字典中整数值的顺序(升序)进行排序。如何实现?

示例:

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"。


2
你能给一个你想要排序的例子吗? - Elastep
1
这个并没有太多意义。你想按每个字典中最小的键排序吗?你必须有一个值来比较不同的字典,并且需要选择哪一个是它。 - Tomislav Markovski
1
你可以使用 SortedList 类。 - vini
你的示例没有展示你的字典和列表之间的关系,就像你在示例中所说的那样,它只是键值对而不是真正的字典。 - Saeed Amiri
@Elastep:那么你如何存储一对值(int-string)? - markzzz
显示剩余7条评论
4个回答

6

如果您想要扁平化数据并对其进行排序,可以尝试以下操作:

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);

1
你可以在定义泛型时使用 SortedDictionary 或 SortedList。这样做,无论你以什么顺序向字典中添加元素,这些元素都将按照键元素(在你的情况下是 int 类型)的顺序存储在已排序的格式中。 此外,SortedDictionary 使用二分搜索树,使其更加高效。

还有一件事,我想知道为什么你使用了5个字典,而一个单独的字典(min1)可以存储所有的键值对。 - LetsKickSomeAss in .net

1

一旦您拥有一个单一的字典,那么您可以使用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<T>,那么:

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

1

然而,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();
    }
}

编程愉快 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接