ProAnswers.org

How to sort C# Dictionary by value?

myList.Sort(
delegate(KeyValuePair<string, string> firstPair,
KeyValuePair<string, string> nextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);

Since you're targeting .net 2.0 or above, you can simplify this into lambda syntax -- it's equivalent but shorter. If you're targeting .net 2.0 you can only use this syntax if you're using the compiler from vs2008 (or above).




	
		
			myList.Sort((firstPair,nextPair) =&gt; 
			    { 
			        return firstPair.Value.CompareTo(nextPair.Value); 
			    } 
			);