Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3606
  • Last Modified:

Help!! Sort Hashtable value

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))
      {
            //slist.Add(locationName, cityCode);
            cities.Add(cityCode, locationName);
      }
}

ANY HELP APPRECIATED!!! THANKS!!!
0
sticar
Asked:
sticar
  • 4
  • 4
  • 3
1 Solution
 
NipNFriar_TuckCommented:
Alright create a comparer class like

    class HTComparer : IComparer {
        #region IComparer Members

        public int Compare(object x, object y) {
            // TODO:  Add HTComparer.Compare implementation
            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
 
sticarAuthor Commented:
Yes I definitely need help... :)
0
 
NipNFriar_TuckCommented:
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) {
            // TODO:  Add HTComparer.Compare implementation
            return String.Compare( x.ToString(), y.ToString() );
        }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AvonWyssCommented:
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
 
sticarAuthor Commented:
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
 
AvonWyssCommented:
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
 
NipNFriar_TuckCommented:
Sorry, I was a little off on the implementation...

do

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

during the constructor...
0
 
sticarAuthor Commented:
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);
                                            cities.Add(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
 
sticarAuthor Commented:
AvonWyss, I ended up using yours.  It works great!  Thanks!
0
 
NipNFriar_TuckCommented:
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))
                         {
                             cities.Add(cityCode, 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
 
AvonWyssCommented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now