Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Help!! Sort Hashtable value

Posted on 2004-10-12
Medium Priority
3,604 Views
I need to sort my hashtable by locationName.  I'm using both values cityCode and locationName in my code.  And locationName can be duplicated.

Hashtable cities=new Hashtable();
//SortedList slist = new SortedList();

for(int i=0, j=1;i<total;i++,j++)
{
string cityCode=list.GetLocation(i).Code;
string locationName=list.GetLocation(i).Location;

if(!cities.ContainsKey(cityCode))
{
}
}

ANY HELP APPRECIATED!!! THANKS!!!
0
Question by:sticar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4
• 3

LVL 7

Expert Comment

ID: 12291308
Alright create a comparer class like

class HTComparer : IComparer {
#region IComparer Members

public int Compare(object x, object y) {
return 0;
}

#endregion

}

Then set the comparer to the hashtable comparer

Hashtable ht = new Hashtable();
ht.comparer = new HTComparer();

If you need help implementing the compare function let me know...
0

Author Comment

ID: 12291350
Yes I definitely need help... :)
0

LVL 7

Expert Comment

ID: 12291405
Realize that object x and object y are just the key values(strings) being passed in and since you are passing in a string you could write something like:

public int Compare(object x, object y) {
return String.Compare( x.ToString(), y.ToString() );
}
0

LVL 14

Expert Comment

ID: 12291462
Er. Why aren't you using a SortedList instead of a Hashtable? This might be what you need, it also offers dictionary fonctionality but it's always sorted. Performance is also quite good (not as fast as a Hashtable, but faster than needing to sort the hashtable contents afterwards).
0

Author Comment

ID: 12291613
AvonWyss: I would still have the issue with sorting the "values" instead of the keys right?  And I can't add the Value as the Key because they can be duplicated.

NipNFriar_Tuck:
I created a class above named HTComparer.cs

Then in another .ascx.cs file I try to call it:
Hashtable cities= new Hashtable();
cities.comparer   //There isn't a "comparer" as an option.
0

LVL 14

Accepted Solution

AvonWyss earned 2000 total points
ID: 12291700
Well, you could work with a SortedList which does not contain strings but lists. This wouls allow you to use Value as key and have multiple associated values.

But there might be another, simpler solution. Try this:

string[] cities=new string[total];
string[] locations=new string[total];
for(int i=0; i<total; i++) {
cities[i]=list.GetLocation(i).Code;
location[i]=list.GetLocation(i).Location;
}
Array.Sort(locations, cities);

This sort by location and also does the same array operations on cities, so that the pairs still match.

Another solution could be to implement your own comparer for the objects or structs returned by GetLocation(), then you could have them in a normal array or an ArrayList. Many possibilities here...
0

LVL 7

Expert Comment

ID: 12292065
Sorry, I was a little off on the implementation...

do

Hashtable ht = new Hashtable( null, new HTComparer() );

during the constructor...
0

Author Comment

ID: 12292360
Does this look close to right? I'm not sure on the sort area, it's only comparing the two values passed in, not all the values in locationName.
--------------------------------------------------------------------
HTComparer cities = new HTComparer();
HTComparer sort = new HTComparer();

for(int i=0;i<total;i++)
{
string cityCode=list.GetLocation(i).Code;
string locationName=list.GetLocation(i).Location;

if(!cities.ContainsKey(locationName))
{
sort.Compare(cityCode, locationName);
}
}

my class
--------------------------------------------------------------------
public class HTComparer :IComparer
{
public HTComparer()
{
Hashtable ht = new Hashtable( null, new HTComparer());
}

public int Compare(object x, object y)
{
return String.Compare( x.ToString(), y.ToString() );
}
}
0

Author Comment

ID: 12297937
AvonWyss, I ended up using yours.  It works great!  Thanks!
0

LVL 7

Expert Comment

ID: 12298333
sticar, your last post was close... here is what it should have looked like...
--------------------------------------------------------------------
Hashtable cities = new Hashtable( null, new HTComparer() );

for(int i=0;i<total;i++)
{
string cityCode=list.GetLocation(i).Code;
string locationName=list.GetLocation(i).Location;

if(!cities.ContainsKey(locationName))
{
}
}

my class
--------------------------------------------------------------------
public class HTComparer :IComparer
{
public int Compare(object x, object y)
{
return String.Compare( x.ToString(), y.ToString() );
}
}

Since a Hashtable sorts keys automagically you do not need to explicitly sort the Hashtable!  The keys are sorted via the comparer that you gave during instantiaion.  By the by this is the alternate solution that AvonWyss gave...  Cheers.
0

LVL 14

Expert Comment

ID: 12298405
NipNFriar_Tuck, since the values must be sorted, not the keys, you suggestion does not solve this problem. Also, note that the hashtable is not sorted by the comparer you gave. It's primary sorting key is the hash code, and for two items which land in the same bucket (that is, where the hashcode modulus the number of buckets is equal), they are sorted with the comparer so that the bucket can be searched with a binary search. But your suggestion with the comparer would work fine for a SortedList, if there wasn't that issue with the multiple values with the same value.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a feâ€¦
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (httpsâ€¦
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Prâ€¦
Suggested Courses
Course of the Month9 days, 14 hours left to enroll