I have this Dictionary
Dictionary<Guid, DateTime> myDic = new Dictionary<Guid, DateTime>();
What is the best approach to sort it DESC by DateTime? Is it LINQ?
How it could be done in code?
Thank you!
You cannot sort a Dictionary without relying on details of current implementation, but you can process its entries in any specific order using LINQ's sorting functionality:
foreach (KeyValuePair<Guid,DateTime> pair in myDict.OrderByDescending(p => p.Value)) {
// do some processing
}
Purely as a curiosity, you can pass ordered key-value pairs to Dictionary's constructor, and the resulting dictionary will enumerate in the order the items were provided in the enumeration to the constructor. However, relying on this functionality is very dangerous; you should not do it in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With