简体   繁体   English

排序列表<dictionary<string,string> > 基于特定的键值</dictionary<string,string>

[英]Sorting a List<Dictionary<string,string>> based on a specific keys value

I have a collections of Dictionary objects.我有一个 collections 的 Dictionary 对象。

Example:例子:

List<Dictionary<string,string>> allData = new List<Dictionary<string,string>>();

I have about 30 entries in each Dictionary object, one of them is a DateTime saved as a string.我在每个 Dictionary object 中大约有 30 个条目,其中一个是保存为字符串的 DateTime。 I want to sort the List of Dictionary objects based on the specific key that has the DateTime field saved as a string in descending/ascending order.我想根据特定键对字典对象列表进行排序,该特定键将 DateTime 字段按降序/升序保存为字符串。

Example Data:示例数据:

Dictionary<string,string> temp = new Dictionary<string,string>();
temp.Add("Key1", "test1");
temp.Add("Key2", "6-12-2019-00:00:00");
// ...
temp.Add("KeyN", "1");

allData .Add(temp);

temp = new Dictionary<string,string>();
temp.Add("Key1", "test2");
temp.Add("Key2", "6-12-2018-06:25:25");
// ...
temp.Add("KeyN", "1");

allData .Add(temp);

temp = new Dictionary<string,string>();
temp.Add("Key1", "test3");
temp.Add("Key2", "12-12-2021-11:59:59");
// ...
temp.Add("KeyN", "1");

allData.Add(temp);

So the List at this point would have 3 dictionary objects.所以此时的列表将有 3 个字典对象。 I want to sorts all of them based on Key2 in ascending or descending order.我想根据 Key2 按升序或降序对所有这些进行排序。 Any help would be appreciated.任何帮助,将不胜感激。

temp.Orderby(dict => DateTime.ParseExact(dict["Key2"], some format));

You can use TryParseExact to check each value in the dictionary for the one which matches a date.您可以使用TryParseExact检查字典中的每个值是否与日期匹配。

The following will give you a sort in place下面就给大家梳理一下

Func<Dictionary<string, string>, DateTime> getDate = dict =>
    dict.Values.FirstOrDefault(v =>
        DateTime.TryParseExact(v, "d-M-yyyy hh:mm:ss", null, 0, out var dat) ? dat : default);

allData.Sort((a, b) => getDate(a).CompareTo(getDate(b)));

This will give you a new list这会给你一个新的列表

var newList = allData.OrderBy(dict =>
    dict.Values.FirstOrDefault(v =>
        DateTime.TryParseExact(v, "d-M-yyyy hh:mm:ss", null, 0, out var dat) ? dat : default
                              ));
IComparer<Dictionary<string, string>> asc = Comparer<Dictionary<string, string>>.Create((d1, d2) => DateTime.Parse(d1["Key2"]).CompareTo(DateTime.Parse(d2["Key2"])));
IComparer<Dictionary<string, string>> desc = Comparer<Dictionary<string, string>>.Create((d1, d2) => DateTime.Parse(d2["Key2"]).CompareTo(DateTime.Parse(d1["Key2"])));
    
allData.Sort(asc);
allData.Sort(desc);

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

相关问题 排序字典 <string, List<int> &gt;基于价值 - Sorting Dictionary <string, List<int>> based on value 在列表中搜索值<dictionary<string, object> > 但使用非特定字符串</dictionary<string,> - Search for a value in List<Dictionary<string, object>> but using a non specific string 根据字符串大小写对列表进行排序 - Sorting a list based on string case 如何检查 List 类型的对象<Dictionary<string, string> &gt; 根据字典中的另一个值设置一个值? - How to check an object of type List<Dictionary<string, string>> to set one value based off another value in the dictionary? 如何使用清单 <string> 作为字典中的键 - How to use a list<string> as the keys in a dictionary 如何订购字典列表 <int, string> 基于Dictionary int值? - How can I order a List of Dictionary<int, string> based on Dictionary int value? 如何对列表进行分组<dictionary<string,string> &gt; 基于特定键并获得最大值</dictionary<string,string> - How to group a List<Dictionary<string,string>> based on particular key and get the max value 有没有办法让 getter 返回特定的字典条目<string, someClass>基于字符串列表? - Is there a way for a getter to return specific dictionary entries<string, someClass> based on a list of strings? 排序列表<list<string> &gt; </list<string> - Sorting List<List<string>> 根据列表值格式化字符串 - Formating a string based on a list value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM