HashTable elements order

  • Thread starter Thread starter Ron Bullman
  • Start date Start date
R

Ron Bullman

Michael,

SortedList or ArrayList (with tailored ICompared) may do the trick for you.

But why are you storing the items to Hashtable in the first place? You might
wanna expose your particular problem a little bit more so guys here in the
NG can give you more educated answers ;-).


Ron
 
I use HashTable to store some information retreived from
database. In database data is stored in some particular
order, that is important for the application.
When I want to display the data (loaded into hashtable)
in ASPX page, user gets data _not_ in the same order as
it was in the database. Is there any way to force
Hashtable to return elements (via iterators) in the same
order as entered [Maybe to implement some interface or
sth..?]. Or maybe there is another, similar class,
holding key-value pairs, that would do this for me.

Thanx for any suggestions
 
i just realized you are RETRIEVING data...is there a
reason you aren't simply using a dataset or other data
object and simply doing a sort on it, or in the proc/sql,
then displaying the data?

-----Original Message-----
Michael,

SortedList or ArrayList (with tailored ICompared) may do the trick for you.

But why are you storing the items to Hashtable in the first place? You might
wanna expose your particular problem a little bit more so guys here in the
NG can give you more educated answers ;-).


Ron
I use HashTable to store some information retreived from
database. In database data is stored in some particular
order, that is important for the application.
When I want to display the data (loaded into hashtable)
in ASPX page, user gets data _not_ in the same order as
it was in the database. Is there any way to force
Hashtable to return elements (via iterators) in the same
order as entered [Maybe to implement some interface or
sth..?]. Or maybe there is another, similar class,
holding key-value pairs, that would do this for me.

Thanx for any suggestions


.
 
-----Original Message-----
i just realized you are RETRIEVING data...is there a
reason you aren't simply using a dataset or other data
object and simply doing a sort on it, or in the
proc/sql, then displaying the data?

Actually there is no _need_ to use Hashtable.
The data loaded into hashTable come from many database
tables and results are produced by several stored
procedures. Only some records from datasets holding
results are copied to hashtables.

It has been decided to use hash table since it allows
very easily map a key to value.
Of course I might change the code, but this would require
quite a lot of work. So that is why I ask wherher there
is a simple method of providing some mechanism into
hashtabe that would allow to return elements in order
that thay came in.
 
I usually create strongly typed collections that inherit from
CollectionBase. It has an InnerList property that stores items by index.
In that strongly typed collection I create a private variable of type
HashTable.

When adding I add to both the InnerList and the HashTable. When getting by
index I get from InnerlList, and when getting by key I get by the HashTable.

CollectionBase is bindable to the built in controls (DataGrid, etc..) in the
same way as a DataSet. It would be best NOT to bind a DataSet directly to a
DataGrid in edit mode if you want to do any business rules validation on the
entered data. However, A strongly typed Collection can contain your
business validation logic.

Performance is comparable to other collections. It may use a little extra
memory, since it stores 2 references (not objects) for each object instead
of 1. What you gain is the ability to reference an item by index or key.

I don't know of any built in type in the collections namespace that does
this with good performance when containing more than 10 items.

I demonstrate this method in an open source project in SourceForge ...
http://sourceforge.net/projects/dbobjecter
....which will generate code for your objects from a database. Use
"Template04" and look at results in the Business layer.

Mike
 
The drawback is that a SortedList is always sorted. The indexes can NOT be
accessed on a "first in first out" basis. Also, the automatic sorting makes
it slower than my approach (according to VS help). ArrayList can Sort using
the "Sort" method. So you sort if/when you want it sorted. The
CollectionBase InnerList property I mentioned is an ArrayList, so it can
Sort too. You just have to expose it in your strongly typed collection.

The SortedList also internally contains 2 arrays making it take just as much
of a memory footprint as my solution.

The SortedList does not implement IList which is required for binding to
controls.

What is needed is a built in collection just like SortedList that does not
automatically Sort as items are added and removed, but that does provide a
Sort method like ArrayList.

Mike
 
Back
Top