leora
leora

Reputation: 196459

can i have an ordered dictionary by date

i have a dictionary where the key is a date and the value is an object. is there anyway i can ensure that when i loop through this collection its in chronological order always even after adding and deleting items.

Upvotes: 3

Views: 1553

Answers (3)

Shay Erlichmen
Shay Erlichmen

Reputation: 31928

You need to use SortedDictionary

Upvotes: 9

Rik
Rik

Reputation: 29243

You can use SortedList: http://msdn.microsoft.com/en-us/library/ms132319.aspx
or SortedDictionary: http://msdn.microsoft.com/en-us/library/f7fta44c.aspx

Upvotes: 2

SLaks
SLaks

Reputation: 887365

You can use LINQ:

foreach(var kvp in dictionary.OrderBy(kvp => kvp.Key)) {
    //Use kvp.Key and kvp.Value
}

Upvotes: 5

Related Questions