473,387 Members | 1,757 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,387 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 43486
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.