排序嵌套字典

时间:2014-04-06 17:15:10

标签: c# sorting dictionary

我想在嵌套字典中按值对字典进行排序。

Dictionary<int, Dictionary<string, string>> Data = new Dictionary<int, Dictionary<string, string>>();

Dictionary<string, string> oneRow = new Dictionary<string,string>();
oneRow.Add("id", "1");
oneRow.Add("name", "John");
oneRow.Add("surname", "Petrucci");
oneRow.Add("data", "2014-01-01");
oneRow.Add("active", "1");
this.Data[1] = oneRow;

oneRow = new Dictionary<string, string>();
oneRow.Add("id", "2");
oneRow.Add("name", "Joe");
oneRow.Add("surname", "Satriani");
oneRow.Add("data", "2014-02-02");
oneRow.Add("active", "1");
this.Data[2] = oneRow;

oneRow = new Dictionary<string, string>();
oneRow.Add("id", "3");
oneRow.Add("name", "Steve");
oneRow.Add("surname", "Vai");
oneRow.Add("data", "2014-03-03");
oneRow.Add("active", "0");
this.Data[3] = oneRow;

如何通过'name'或'surname'DESC?

对this.Data进行排序

我正在搜索,但我找不到解决方案。

1 个答案:

答案 0 :(得分:1)

这是最简短的方法:

Data = Data.OrderByDescending(x => x.Value["name"])
           .ToDictionary(x => x.Key, x => x.Value);