473,805 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Order Hastable by value

Hello
I have a Hastable, which I need to order it by value and not by key.

Regards
Nov 15 '05 #1
2 3135
n!
> Hello
I have a Hastable, which I need to order it by value and not by key.

Regards


Maybe I'm missing something, a hashtable is indexed by the key. What
difference does the ordering of the values do? :)
If you're trying to enumerate through the collection and require them to
arrive in a specific order then I'd suggest a Hashtable is not what you
want.

n!
Nov 15 '05 #2
"n!" <nf********@nom ailplease.com> wrote in message
If you're trying to enumerate through the collection and require them to
arrive in a specific order then I'd suggest a Hashtable is not what you
want.

To elaborate a little more... perhaps you should make a class of two public
values: key and value:

public class KeyValPair
{
public object Key;
public object Val;
}

If you are frequently sorting/searching by value (and not key) then I would
probably use an ArrayList of KeyValPair for the entire app. You can make an
implementation of IComparer (something like below) to use with
ArrayList.Sort( ) for sorting by Val. You can also look into making your own
collection from CollectionBase or DictionaryBase.

public class ValueComparer : IComparer
int CompareValues (object x, object y)
{
KeyValPair xPair = x as KeyValPair;
KeyValPair yPair = y as KeyValPair;
// check for nulls, then compare xPair.Val and yPair.Val
return // the result
}

Or you can stick with your hashtable, and then when you want the sorted
values, loop through the Hashtable and add to the SortedList, possibly as
follows (although this seems really backwards to me):

SortedList list = new SortedList (hashtable.Coun t);
foreach (object key in hashtable.Keys)
{
list.Add (hashtable[key], key); // add the key-value pair as a value-key
pair - confusing...
}

How your app generally uses the collection and how much you want to optomize
for development time vs. performance will affect the method you should
probably take.

--
Mike Mayer - Visual C# MVP
http://www.mag37.com/csharp/
mi**@mag37.com


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3673
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
0
2161
by: Cliff | last post by:
Can someone tell me ...show me how to I would go about creating a Hastable in a HashTable. Say I have items A B C D. I want to be able to call item A the key, and it will give me back B C and D. I'm not sure how do this. thanks.
3
1455
by: Ajay | last post by:
I have a global hashtable instance in my application which is accessed by threads within that application for readonly access. Do I need to synchronize the hashtable if readonly access is required? Will the multiple threads be able to search the hastable simultaneously ? Thanks Ajay
3
3536
by: Girish | last post by:
How do i populate a static hastable without the need to instantiate the class? any ideas? Im confused. class VAErrorMap { static Hasttable _ErrorMap = new Hashtable(); public VAErrorMap() { //populate has here.. but dont want to!
3
3740
by: nevin | last post by:
Hello all, I've looked on the net and can't seem to find what I need so I must be searching badly or have (another) 'non-problem'. I am simply trying to move the values of a hastable or sorted list into a combobox control. I'm willing to lose the key item of the collection I just need the values. Surely i don't need to enumerate the entire collection adn add the values one by one into the combobox?
4
25971
by: Nadav | last post by:
Hello. I have a Hashtable, to which I insert key and values. When I go over the hashtable with "foreach" later on, the items are not coming out at the order I inserted them. A quickwatch showed that as the items are inserted, they are not arranged in the hash at the order they were inserted by. Is there a way to control that with a hashtable ? I can't even sort it... Thanks
9
7256
by: nithya4u | last post by:
Hi All, Currently, am working on converting some part of C# code to JAVA. Am having a problem with respect to the usage of Hashtable in C#. Is there a java equivalent for it? I have searched lots and am not able to get a solution. In the C# code that am working on, there is an arraylist and they are copying the values to a hashtable for easy retrieval purpose. After making a copy, ironically, the order of values in both the arraylist...
16
2459
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? -> array2 is assigned to array1 ....???? the reason being it is
5
1662
by: RSH | last post by:
I have two HashTables (_CompanyDeductions,_CompanyAccruals) that contain several objects each. I am trying to loop through and print the properties of the objects in the Hashtables: I am trying to creat a dynamic function that will print all of the properties of each of the objects stored in the the HashTable. I can't seem to get at the properties of the objects. Thanks for any help! Ron
0
9718
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
10363
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...
0
10109
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
9186
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
7649
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
6876
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();...
1
4327
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
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.