简体   繁体   English

如何根据日期键值对字典进行排序

[英]How to sort a dictionary based on date key value

I have a dictionary, which is a nested dictionary.我有一本字典,它是一个嵌套字典。 I want to sort it according to the date and time provided in the dictionary.我想根据字典中提供的日期和时间对其进行排序。 how to do this is c#. Here is my code如何做到这一点是 c#。这是我的代码

foreach (var user in Load_User)
            {
                
                if(user.Value.Projects[0].type=="0")
                {
                    print("Standard User");
                    
                    
                     _Standard_User.Add(user.Key, new Dictionary<string, string>() { 
                               { "Email", user.Value.Email },
                               { "age",user.Value.age},
                               {"userName", user.Value.userName},
                               {"mobile", user.Value.mobile},
                               {"instagram_handle",user.Value.instagram_handle},
                               { "City_Of_Residence", user.Value.City_Of_Residence },
                               { "nationality", user.Value.nationality },
                               { "No_of_Votes", user.Value.No_of_Votes.ToString()},
                               {"Brief",user.Value.Projects[0].Brief},
                               {"Creation_Date", user.Value.Projects[0].Creation_Date},
                               {"Creation_Time", user.Value.Projects[0].Creation_Time},
                               {"type", user.Value.Projects[0].type},
                               {"localId",user.Value.localId},
                               {"thumbnail_image",user.Value.Projects[0].thumbnail_image}
                            });

                }   

You can do this using LINQ (make sure to include the System.Linq namespace):您可以使用 LINQ 执行此操作(确保包含System.Linq命名空间):

_Standard_User.OrderBy(x => Convert.ToDateTime(x.Value["Creation_Date"]).Add(TimeSpan.Parse(x.Value["Creation_Time"])))
              .ToDictionary(x => x.Key, x => x.Value);

This will return the dictionary, sorted by both the creation date and time.这将返回字典,按创建日期和时间排序。 This expects the Creation_Date to be parseable by Convert.ToDateTime (a format like 2022-09-30), and Creation_Time to be parseable by TimeSpan.Parse (a format like 10:38).这期望Creation_Date可由Convert.ToDateTime (格式如 2022-09-30)解析, Creation_Time可由TimeSpan.Parse (格式如 10:38)解析。

If you have the exact format, you can utilize TimeSpan.ParseExact which will bypass any culture issues:如果您有确切的格式,您可以使用TimeSpan.ParseExact来绕过任何文化问题:

_Standard_User.OrderBy(x => DateTime.ParseExact(x.Value["Creation_Date"], "yyyy-MM-dd", null).Add(TimeSpan.Parse(x.Value["Creation_Time"])))
              .ToDictionary(x => x.Key, x => x.Value);

The snippet above will be functionally equal to the previous snippet, but it will expect dates in the format of year-month-day.上面的代码片段在功能上与前面的代码片段相同,但它期望日期格式为年-月-日。

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

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