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 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
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
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);
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 .....}
|
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
|
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:
|
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
|
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)
| |
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
|
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
|
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.
|
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.
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |