繁体   English   中英

如何订购字典列表 <int, string> 基于Dictionary int值?

[英]How can I order a List of Dictionary<int, string> based on Dictionary int value?

我有这个清单:

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

我想对列表进行排序,所以当我循环它并且我得到字符串我打印“你好我的名字是marco”

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

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

定义泛型时,可以使用SortedDictionary或SortedList。 当您这样做时,无论您以什么顺序向Dictionary添加元素,元素都将以基于key-element(在您的情况下为int Type)的排序格式存储。 此外,SortedDictionary使用BinarySearch树,这使它更有时间效率。

一旦你有一个字典,那么你可以使用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);

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

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

快乐编码:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM