473,770 Members | 3,398 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 43557
have you looked at the generic SortedDictionar y 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******@despa mmed.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<Timpelemen tation to store your data objects, then use
the Sort() method. For example:

List<MyClassm_l ist = new List<MyClass>() ;
m_list.Add(inst ance1);
m_list.Add(inst ance2);
m_list.Sort({Ge neric 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
SortableBinding List 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 SortedDictionar y class

http://msdn2.microsoft.com/en-us/library/f7fta44c.aspx
Sure, but the SortedDictionar y 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<stri ng, strings = new Dictionary<stri ng, string>();
s.Add("1", "a Item");
s.Add("2", "c Item");
s.Add("3", "b Item");

List<KeyValuePa ir<string, string>myList = new
List<KeyValuePa ir<string, string>>(s);
myList.Sort(
delegate(KeyVal uePair<string, stringfirstPair ,
KeyValuePair<st ring, stringnextPair)
{
return firstPair.Value .CompareTo(next Pair.Value);
}
);

foreach (KeyValuePair<s tring, 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******@despa mmed.comwrote in message
news:ej******** **@newsreader2. netcologne.de.. .
John Timney (MVP) schrieb:
>have you looked at the generic SortedDictionar y class

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

Sure, but the SortedDictionar y 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 IterSortHashVal ue 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<stri ng, strings = new Dictionary<stri ng, string>();
s.Add("1", "a Item");
s.Add("2", "c Item");
s.Add("3", "b Item");

List<KeyValuePa ir<string, string>myList = new
List<KeyValueP air<string, string>>(s);
myList.Sort(
delegate(KeyVal uePair<string, stringfirstPair ,
KeyValuePair<s tring, stringnextPair)
{
return firstPair.Value .CompareTo(next Pair.Value);
}
);

foreach (KeyValuePair<s tring, 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
1859
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. There are 'n' itemsets, the size of n is unknown. The program can count itself while scanning. Each of the n itemset is of the form key: value {key1: value1, key2:value2, key3, value3, key4: value4 .....}
4
2533
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() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
4
2898
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 extracted EXIF value from the following set: data element of v:
5
2088
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
3266
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)) CType(local4, Short) = CType(src, Short)
5
2308
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
2271
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
7807
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 application ie sometimes I want to cycle through the values in the collection in DateTime order while at other times in TimeSpan order. Ideally I would like multiple keys - such as Timespan within DateTime order but maybe that is asking too much.
1
1505
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 get it right.
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10057
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10002
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.