473,657 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"Sorting" a hashtable

I have a hashtable (h) of class objects keyed by a string (k) - the class
(c) object stored in each hastable entry has two attributes (a1 and a2).

I want to iterate through the entries of the hashtable by ordering by one of
the attributes of the class (a1 or a2).

How can this easily be achieved ? I think I have to copy to an ArrayList
then write an IComparer - but I'm not sure if this is the best way.

Any advice appreciated.

Nov 16 '05 #1
4 5221
You can instantiate the ArrayList from the Keys property of the hashtable.
Then sort the ArrayList (no need to write a comparer). You can then iterate
through the keys in the arraylist and lookup the associated value in the
hashtable.

If you don't want that final step (coz of performance), you could write a
comparer for the object (assuming the key is also stored in the object),
instantiate the ArrayList from the Values property, and then sort using your
custom comparer. The ArrayList will then contain a sorted list of your
objects.

"JezB" <je***********@ blueyonder.co.u k> wrote in message
news:eD******** ******@tk2msftn gp13.phx.gbl...
I have a hashtable (h) of class objects keyed by a string (k) - the class
(c) object stored in each hastable entry has two attributes (a1 and a2).

I want to iterate through the entries of the hashtable by ordering by one of the attributes of the class (a1 or a2).

How can this easily be achieved ? I think I have to copy to an ArrayList
then write an IComparer - but I'm not sure if this is the best way.

Any advice appreciated.

Nov 16 '05 #2
Won't that just order by the hashtable key ? I want to order by an attribute
in the hashtable values.

"John Wood" <sp**@isannoyin g.com> wrote in message
news:eP******** ******@TK2MSFTN GP10.phx.gbl...
You can instantiate the ArrayList from the Keys property of the hashtable.
Then sort the ArrayList (no need to write a comparer). You can then iterate through the keys in the arraylist and lookup the associated value in the
hashtable.

If you don't want that final step (coz of performance), you could write a
comparer for the object (assuming the key is also stored in the object),
instantiate the ArrayList from the Values property, and then sort using your custom comparer. The ArrayList will then contain a sorted list of your
objects.

"JezB" <je***********@ blueyonder.co.u k> wrote in message
news:eD******** ******@tk2msftn gp13.phx.gbl...
I have a hashtable (h) of class objects keyed by a string (k) - the class (c) object stored in each hastable entry has two attributes (a1 and a2).

I want to iterate through the entries of the hashtable by ordering by
one of
the attributes of the class (a1 or a2).

How can this easily be achieved ? I think I have to copy to an ArrayList
then write an IComparer - but I'm not sure if this is the best way.

Any advice appreciated.


Nov 16 '05 #3
Ah, now that I've read your question properly...... ;)
Yeah sorry about that. So the attribute has some kind of parameter that
would become the sort key?
I think you will need to write a custom comparer, as the only way to get
hold of the attributes is through the type.

"JezB" <je***********@ blueyonder.co.u k> wrote in message
news:O5******** ******@TK2MSFTN GP11.phx.gbl...
Won't that just order by the hashtable key ? I want to order by an attribute in the hashtable values.

"John Wood" <sp**@isannoyin g.com> wrote in message
news:eP******** ******@TK2MSFTN GP10.phx.gbl...
You can instantiate the ArrayList from the Keys property of the hashtable.
Then sort the ArrayList (no need to write a comparer). You can then

iterate
through the keys in the arraylist and lookup the associated value in the
hashtable.

If you don't want that final step (coz of performance), you could write a comparer for the object (assuming the key is also stored in the object),
instantiate the ArrayList from the Values property, and then sort using

your
custom comparer. The ArrayList will then contain a sorted list of your
objects.

"JezB" <je***********@ blueyonder.co.u k> wrote in message
news:eD******** ******@tk2msftn gp13.phx.gbl...
I have a hashtable (h) of class objects keyed by a string (k) - the

class (c) object stored in each hastable entry has two attributes (a1 and a2).
I want to iterate through the entries of the hashtable by ordering by

one
of
the attributes of the class (a1 or a2).

How can this easily be achieved ? I think I have to copy to an ArrayList then write an IComparer - but I'm not sure if this is the best way.

Any advice appreciated.



Nov 16 '05 #4
That's what I ended up having to do.

"John Wood" <sp**@isannoyin g.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ah, now that I've read your question properly...... ;)
Yeah sorry about that. So the attribute has some kind of parameter that
would become the sort key?
I think you will need to write a custom comparer, as the only way to get
hold of the attributes is through the type.

"JezB" <je***********@ blueyonder.co.u k> wrote in message
news:O5******** ******@TK2MSFTN GP11.phx.gbl...
Won't that just order by the hashtable key ? I want to order by an attribute
in the hashtable values.

"John Wood" <sp**@isannoyin g.com> wrote in message
news:eP******** ******@TK2MSFTN GP10.phx.gbl...
You can instantiate the ArrayList from the Keys property of the hashtable. Then sort the ArrayList (no need to write a comparer). You can then

iterate
through the keys in the arraylist and lookup the associated value in the hashtable.

If you don't want that final step (coz of performance), you could write a
comparer for the object (assuming the key is also stored in the
object), instantiate the ArrayList from the Values property, and then sort
using your
custom comparer. The ArrayList will then contain a sorted list of your
objects.

"JezB" <je***********@ blueyonder.co.u k> wrote in message
news:eD******** ******@tk2msftn gp13.phx.gbl...
> I have a hashtable (h) of class objects keyed by a string (k) - the

class
> (c) object stored in each hastable entry has two attributes (a1 and a2). >
> I want to iterate through the entries of the hashtable by ordering
by one
of
> the attributes of the class (a1 or a2).
>
> How can this easily be achieved ? I think I have to copy to an

ArrayList > then write an IComparer - but I'm not sure if this is the best way.
>
> Any advice appreciated.
>
>
>



Nov 16 '05 #5

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

Similar topics

10
12835
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following SQL commands will return the same result but one of my friends don't think so. He said "QUERY 1" will return 1 unsorted record (ROWNUM < 2 ) first then sort the result (ORDER BY COL1 ASC,
3
1759
by: Michal Grabowski | last post by:
Hello, I need to have a stored procedure, which performs sorting. Something like this: CREATE PROCEDURE procname @sortby varchar(30) AS BEGIN SELECT some, columns FROM some_table
15
2647
by: Marc Le Roy | last post by:
Hello, ADA Ravenscar is a restricted subset of the ADA language that has been defined for real-time software development in safety critical applications. Completed with additional restrictions like the ones defined in the SPARK profile, it allow to build very deterministic applications that support automatic static code analysis and schedulability analysis. http://www.acm.org/pubs/articles/proceedings/ada/289524/p1-dobbing/p1-dobbing.pdf...
6
5608
by: peter pilsl | last post by:
postgres 7.3.2 I store unicode-data in postgresql. The data is retrieved via webinterfaces, processed with perl and then stored in postgresql (and viceversa). All is going nice with one problem. If performing a "select * order by field"-query the result is not what I expected. German umlauts (ie: Ö) are stored as doublechars ("Ö" is "Ö") and only the first char seems to be taken into account when sorting.
3
2730
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have reports at the plan (overall totals), department and division levels which have sorting and grouping implemented with this new
3
1513
by: kartik | last post by:
Hi, I have a huge table with ID, Name and other info. ID can be either 1 or 2 or 3. I wish to create a query which sorts the data by ID, but in this order 2,1 and then 3. Is there any way to use Order by(or any other option) to achieve this? Any help would be appreciated. Thanks
10
17209
by: Mart | last post by:
What class does everyone out there use if they want to store a set of values efficiently? In java I use a HashSet, but there is no equivalent in C#. Even worse, the lowest level interface to define the Add, Contains and Remove methods is an IList, and we all should know that a set isn't a list. ICollection only defines Count and CopyTo, which isn't useful at all. So even if I want to write my own Set implementation, what interface...
2
2124
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I have an array of structs containing data which I'd like to output ordered based on the value of a single member. I was wondering if there is a relatively simple way of doing this without actually modifying the structure of the array? I had a...
33
2310
by: Frederick Gotham | last post by:
(My dialect of English seems to puzzle people at times, so I'll first clarify a few terms which I use in the following post:) (1) By "domestic", I mean "ordinary, run-of-the-mill, not extraordinary or strange". (2) By "willy-nilly", I mean something along the lines of "haphazardly", but without any sense of recklessness (i.e. gleefully doing something without expecting any sort of negative effect, even though there may in fact be a...
0
8407
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
8739
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
8612
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...
1
6175
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
5638
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
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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.