using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Dictionary<string, string> objCountries = new Dictionary<string, string>();
//Adding countries to dictionary in random order
objCountries.Add("India", "India");
objCountries.Add("America", "America");
objCountries.Add("Canada", "Canada");
objCountries.Add("Britain", "Britain");
objCountries.Add("New Zealand", "New Zealand");
objCountries.Add("Nepal", "Nepal");
objCountries.Add("Myanmar", "Myanmar");
objCountries.Add("China", "China");
objCountries.Add("Singapore", "Singapore");
//Sorting the countries in the dictionary object based on value
List<KeyValuePair<string, string>> mySortedList = objCountries.ToList();
mySortedList.Sort(
delegate(KeyValuePair<string, string> firstPair,
KeyValuePair<string, string> nextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);
}
}This code explains about how to sort dictionary object based on value.
