TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Sort Dictionary Object based on Value
Kannadasan G
May 12
2015
Code
1
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
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);
}
);
}
}
Dictionary
Sort Dictionary Object