473,407 Members | 2,326 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,407 software developers and data experts.

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 3119
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********@nomailplease.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.Count);
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
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....
0
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....
3
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?...
3
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() {...
3
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...
4
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...
9
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...
16
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? ...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
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...
0
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,...
0
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...

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.