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.