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

Help! Need to sorted collection accessible by key

I need to create a collection of classes (or structures) can be
accessed by a string key, eg MyColl("ShortName5").Name for class with
key ShortName5. But it also has to be sorted by a second ordering
field.

Hashtable offers access by key, but is not sorted. Arraylist is sorted
(the fields will come presorted from the db), but I can't access by
key. SortedList can't be sorted by a field different from the key
field.

Can anyone think of a solution? Thanks for any help. Using .Net 1.1.

Burt

Nov 21 '06 #1
6 1855
KH
Create your own type for the key that implements IComparable, and override
Equals() and GetHashCode(), then return the hashcode of the key you want
access with, and compare (sort) by the one you want to order by, and put it
in SortedList; quickly:

class MyKey : IComparable
{
MyKey(string comparer, string hasher)
{
this._comparer = comparer;
this._hasher = hasher;
}

override bool Equals(MyKey other)
{
return (this._comparer == other._comparer);
}

override int GetHashCode()
{
return this._hasher.GetHashCode();
}

int CompareTo(MyKey other)
{
return this._comparer.CompareTo(other._comparer);
}
};

"Burt" wrote:
I need to create a collection of classes (or structures) can be
accessed by a string key, eg MyColl("ShortName5").Name for class with
key ShortName5. But it also has to be sorted by a second ordering
field.

Hashtable offers access by key, but is not sorted. Arraylist is sorted
(the fields will come presorted from the db), but I can't access by
key. SortedList can't be sorted by a field different from the key
field.

Can anyone think of a solution? Thanks for any help. Using .Net 1.1.

Burt

Nov 21 '06 #2
Hi Burt,

Check -
http://www.codeproject.com/csharp/hashlistarticle.asp

HTH

Cheers,
Chester

"Burt" wrote:
I need to create a collection of classes (or structures) can be
accessed by a string key, eg MyColl("ShortName5").Name for class with
key ShortName5. But it also has to be sorted by a second ordering
field.

Hashtable offers access by key, but is not sorted. Arraylist is sorted
(the fields will come presorted from the db), but I can't access by
key. SortedList can't be sorted by a field different from the key
field.

Can anyone think of a solution? Thanks for any help. Using .Net 1.1.

Burt

Nov 21 '06 #3

KH wrote:
Create your own type for the key that implements IComparable, and override
Equals() and GetHashCode(), then return the hashcode of the key you want
access with, and compare (sort) by the one you want to order by, and put it
in SortedList; quickly:

class MyKey : IComparable
{
MyKey(string comparer, string hasher)
{
this._comparer = comparer;
this._hasher = hasher;
}

override bool Equals(MyKey other)
{
return (this._comparer == other._comparer);
}

override int GetHashCode()
{
return this._hasher.GetHashCode();
}

int CompareTo(MyKey other)
{
return this._comparer.CompareTo(other._comparer);
}
};
This is unlikely to work, or at least will work under restricted
circumstances.

Hashtable uses the Equals method to decide if two items are equal,
since GetHashCode may return the same code for more than one item.

So, this will work ONLY if the _comparer and the _hasher are such that
if two items compare as equal by the _comparer key then they also
compare equal by the _hasher key. Let's look at what happens if this is
not true.

If two items that compare equal using the _hasher key do not compare
equal with the _comparer key, then Hashtable will put duplicate entries
in the table for that item, since the second item to be hashed will
hash to the same bucket in the hash table but will not be equal to
anything in the chain. So this:

MyKey key1 = new MyKey("A", "A");
MyKey key2 = new MyKey("B", "A");
myHashtable[key1] = obj1;
myHashtable[key2] = obj2;

may result in two entries in the hash table even though the "hasher"
key of both key1 and key2 are the same.

Conversely, if two items that compare equal using the _comparer key do
not compare equal with the _hasher key, then they may result in only
one entry in the Hashtable if by chance the two _hasher keys hash to
the same code (and thus the same hash table bucket), since the second
object to be hashed will be equal to an item already in the chain.

I would suggest instead that you create your own data structure backed
by a Hashtable and either a SortedList or your own sorted list
implementation. Here's a rough idea:

public class MyCollection
{
private Hashtable _hash;
private SortedList _list;

public MyCollection(int capacity)
{
this._hash = new Hashtable(capacity);
this._list = new SortedList(capacity);
}

public MyCollection() : this(10) { }

public object this[string hashKey]
{
get { return this._hash[hashKey]; }
}

public object this[int index]
{
get { return this._list[index]; }
}

public IDictionaryEnumerator GetEnumerator() { return
this._list.GetEnumerator(); }

public object this[string hashKey, string listKey]
{
get
{
object hashObj = this._hash[hashKey];
object listObj = this._list[listKey];
if (hashObj != listObj) { throw new
ArgumentException(String.Format("Hash key '{0}' and list key '{1}'
indicate different elements.", hashKey, listKey), "listKey"); }
return hashObj;
}

set
{
this._hash[hashKey] = value;
this._list[listKey] = value;
}
}
}

Of course, you can define more methods, and the semantics are really up
to you. (For example, if you say myStructure["key"] do you mean the
hash table key or the list key? Or perhaps the caller must always
specify both keys? Or perhaps you supply functions, not indexers, to
allow the caller to look in either table?) This was just a quick sketch
of what is possible.

Nov 21 '06 #4
SortedList also has GetByKey that may be of interest

Marc

Nov 21 '06 #5
Oops; meant GetByIndex!! the indexer (this[object]) provides "by key"

Marc

Nov 21 '06 #6
Thanks everyone for the great ideas. I ended up trying to create my
own key class for a couple of hours, but couldn't make it work (kept
getting IComparer errors when accessing by key, even though the key
class implemented IComparable. I think the sorted list was looking to
the object, not the key).

I ended up just holding a second arraylist in memory in the right order
and accessing the sortedlist by looping through that. I believe .net
2.0 has a collection that will solve the problem when I upgrade.

Thanks again,

Burt


Marc Gravell wrote:
Oops; meant GetByIndex!! the indexer (this[object]) provides "by key"

Marc
Nov 24 '06 #7

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
4
by: dixie | last post by:
Help, I'm really out of my depth here (not unusual I hear you say :-). I have just installed HTML Help in an application. I told it in the Project Properties the path to the help file. I then...
9
by: JJ | last post by:
Do you all use HTML help workshop to create your help system. I am finding it quite clumsy to use. Mayeb because I am not used to using it. Do any of you use any other techniques to create help...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
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
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...

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.