473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Sorting Dictionary by Values

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
5 43488
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 Pöpping" <ma******@despammed.comwrote in message
news:ej**********@newsreader2.netcologne.de...
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 #2
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
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
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 Pöpping" <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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: shalendra chhabra | last post by:
Hi, I just had a tryst with python. I was wondering if python is good enough to do this kind of job -- for it has extensive support of string and pattern matching, ordering and list handling. ...
4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
4
by: Rory Campbell-Lange | last post by:
I have a dictionary of images. I wish to sort the dictionary 'v' by a dictionary value using python 2.3. The dictionary value is the date attribute as shown here: v This attribute is an...
5
by: Rakesh | last post by:
Hi, For a particular problem of mine, I want to sort <key, value> pairs by its value. Eg: Input: A, 4 B, 5
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
5
by: JerryB | last post by:
Hi, I have a dictionary for counting ocurrences of strings in a document. The dictionary looks like this: 'hello':135 'goodbye':30 'lucy':4 'sky':55 'diamonds':239843 'yesterday':4
8
by: nidhog | last post by:
Hello guys, I made a script that extracts strings from a binary file. It works. My next problem is sorting those strings. Output is like: ---- snip ---- 200501221530
11
by: garyhoran | last post by:
Hi Guys, I have a collection that contains various attributes (stuff like strings, DateTime and Timespan) . I would like to access the collection in various orders at different points in the...
1
by: jim-on-linux | last post by:
On Saturday 14 June 2008 03:15, Beema shafreen wrote: I have used this method to solve similar problems. This is a consept of how to do what you want, but you will have to work a little to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.