Question : Sort a SortedDictionary based on value, not key - C#.net MVC

I have a SortedDictionary, the key is a long value and the matching value to each key is a class. The class contains a string and a datetime.


I need to sort my SortedDictionary based on the datetime in my class. So that when I do a foreach to loop through my SortedDictionary I will have them sorted based on datetime.


Is this possible? How can I achieve it?


Cheers

Answer : Sort a SortedDictionary based on value, not key - C#.net MVC

@mc101

First off, sorry I'm a VB guy. But here goes:

You could make a class which implements IComparable or IComparable(Of T): http://msdn.microsoft.com/en-us/library/system.icomparable.aspx

Override the different methods, so it 'knows' how to sort the datetime property in your custom class.

Then call the .Sort method on the Values collection and throw in the class which implements IComparable as a parameter in the Sort Method.

I followed this tutorial to learn how to do this: http://www.java2s.com/Code/VB/Data-Structure/IComparerDemocustomsorting.htm

Hope this helps.
Random Solutions  
 
programming4us programming4us