473,396 Members | 1,738 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,396 software developers and data experts.

"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 5208
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.uk> wrote in message
news:eD**************@tk2msftngp13.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**@isannoying.com> wrote in message
news:eP**************@TK2MSFTNGP10.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.uk> wrote in message
news:eD**************@tk2msftngp13.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.uk> wrote in message
news:O5**************@TK2MSFTNGP11.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**@isannoying.com> wrote in message
news:eP**************@TK2MSFTNGP10.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.uk> wrote in message
news:eD**************@tk2msftngp13.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**@isannoying.com> wrote in message
news:%2****************@TK2MSFTNGP09.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.uk> wrote in message
news:O5**************@TK2MSFTNGP11.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**@isannoying.com> wrote in message
news:eP**************@TK2MSFTNGP10.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.uk> wrote in message
news:eD**************@tk2msftngp13.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
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...
3
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
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...
6
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...
3
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...
3
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...
10
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...
2
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...
33
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...

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.