By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,500 Members | 2,809 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,500 IT Pros & Developers. It's quick & easy.

Sorting Dictionary by Values

P: n/a
Hello,

I´ve a simple question about existing data structures/ collections.

Is there a way to sort a dictionary (or any comparable collection/ data
structure) by values instead of by keys?

For example if you have a dictionary with words as keys and the
frequency of these words in a text as values.

What is the most performant way to sort these dictionary by the
frequency of the words (=values)?

Regards,

Martin
Nov 14 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
have you looked at the generic SortedDictionary class

http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx

--
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
http://www.johntimney.com/blog
"Martin Ppping" <ma******@despammed.comwrote in message
news:ej**********@newsreader2.netcologne.de...
Hello,

Ive a simple question about existing data structures/ collections.

Is there a way to sort a dictionary (or any comparable collection/ data
structure) by values instead of by keys?

For example if you have a dictionary with words as keys and the frequency
of these words in a text as values.

What is the most performant way to sort these dictionary by the frequency
of the words (=values)?

Regards,

Martin

Nov 14 '06 #2

P: n/a
Two options:

1) Use a List<Timpelementation to store your data objects, then use
the Sort() method. For example:

List<MyClassm_list = new List<MyClass>();
m_list.Add(instance1);
m_list.Add(instance2);
m_list.Sort({Generic Comparison});

http://msdn2.microsoft.com/en-gb/library/w56d4y5z.aspx

2) Implement a custom sortable binding list implementation (if you need
binding as well). There is a good article on creating a
SortableBindingList in MSDN:
http://msdn.microsoft.com/library/de...ms02182005.asp

Bill

Nov 14 '06 #3

P: n/a
John Timney (MVP) schrieb:
have you looked at the generic SortedDictionary class

http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx
Sure, but the SortedDictionary sorts by Key and I want to sort
everything by value
Nov 14 '06 #4

P: n/a
oops - so it does.

Heres some code to sort the contents of the dictionary based on its values,
having key and vlaues both of type string. See if you can work with that.

Dictionary<string, strings = new Dictionary<string, string>();
s.Add("1", "a Item");
s.Add("2", "c Item");
s.Add("3", "b Item");

List<KeyValuePair<string, string>myList = new
List<KeyValuePair<string, string>>(s);
myList.Sort(
delegate(KeyValuePair<string, stringfirstPair,
KeyValuePair<string, stringnextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);

foreach (KeyValuePair<string, stringmyKey in myList)
{
Response.Write(myKey.Key + " " + myKey.Value);
}
--
Regards

John Timney (MVP)
VISIT MY WEBSITE:
http://www.johntimney.com
http://www.johntimney.com/blog
"Martin Ppping" <ma******@despammed.comwrote in message
news:ej**********@newsreader2.netcologne.de...
John Timney (MVP) schrieb:
>have you looked at the generic SortedDictionary class

http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx

Sure, but the SortedDictionary sorts by Key and I want to sort everything
by value

Nov 14 '06 #5

P: n/a
This article by Eric Gunnerson at Microsoft was very helpful for
creating a sorted, enumerable hash table:
http://tinyurl.com/yj2x8a
I made a minor tweak to the IterSortHashValue code to return an Object
which can be used like a Hashtable for DropDownLists which exposes
both a Key and a Value.

I haven't compared any of these techniques and don't know if the OP
really needs to have a Dictionary or if that's just what he happens to
be looking at, at the moment.

HTH
"John Timney \(MVP\)" wrote:
>oops - so it does.

Heres some code to sort the contents of the dictionary based on its values,
having key and vlaues both of type string. See if you can work with that.

Dictionary<string, strings = new Dictionary<string, string>();
s.Add("1", "a Item");
s.Add("2", "c Item");
s.Add("3", "b Item");

List<KeyValuePair<string, string>myList = new
List<KeyValuePair<string, string>>(s);
myList.Sort(
delegate(KeyValuePair<string, stringfirstPair,
KeyValuePair<string, stringnextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);

foreach (KeyValuePair<string, stringmyKey in myList)
{
Response.Write(myKey.Key + " " + myKey.Value);
}
Nov 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.