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
Sort Dictionary in C# Using OrderBy
Jeetendra Gund
Oct 09
2014
Code
4.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
using
System;
using
System.Collections.Generic;
using
System.Linq;
namespace
TestCalculator
{
class
Program
{
static
void
Main(
string
[] args)
{
Console.WriteLine(
"/* life runs on code */"
);
//Dictionary.
var dictionary =
new
Dictionary<
string
,
int
>(5);
dictionary.Add(
"Anil"
, 1);
dictionary.Add(
"Amol"
, 0);
dictionary.Add(
"Pravin Singh"
, 5);
dictionary.Add(
"Jeet"
, 3);
dictionary.Add(
"Sadhana"
, 2);
//Using Order by values.
//LINQ to specify sorting by value.
var sorts = from pair
in
dictionary
orderby pair.Value ascending
select pair;
// Display results.
foreach
(var sort
in
sorts)
{
Console.WriteLine(
"{0}: {1}"
, sort.Key, sort.Value);
}
}
}
}
C#
Dictionary Collection
sort Dictionary