473,804 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is there a way to sort a hashtable?

Hello,

Let's say that we have a hashtable with some person objects.
This persons have a name.

Now I want to sort the people objects inside the hashtable by name.
How can the hashtable do this for me?

Thanks!
Nov 15 '05 #1
5 7731
Arjen <bo*****@hotmai l.com> wrote:
Let's say that we have a hashtable with some person objects.
This persons have a name.

Now I want to sort the people objects inside the hashtable by name.
How can the hashtable do this for me?


You can't - a hashtable is effectively a set, not a sequence: it
doesn't have an order.

What you *can* do is:

ArrayList al = new ArrayList (hashtable.Valu es);
al.Sort (myNameComparat or);

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
Alternatively, there is the SortedList class which is a hybrid between
the Hashtable and ArrayList. (it sorts based on the key)

Although, it sounds as if your Hashtable stores the Person objects as
the value in which case this wouldn't work.

Nonetheless, now you know there's a SortedList. :)

On Fri, 9 Jan 2004 22:33:31 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co m> wrote:
Arjen <bo*****@hotmai l.com> wrote:
Let's say that we have a hashtable with some person objects.
This persons have a name.

Now I want to sort the people objects inside the hashtable by name.
How can the hashtable do this for me?


You can't - a hashtable is effectively a set, not a sequence: it
doesn't have an order.

What you *can* do is:

ArrayList al = new ArrayList (hashtable.Valu es);
al.Sort (myNameComparat or);


Nov 15 '05 #3
100
Hi Arjen,
As long as hashtables are not ordered collections you cannot sort them.
However, you can maintain a separate list along with the hashtable which
will keep the order of the elements and you can sort this list and use it
latter to retreive the elements.
Take a look on System.Collecti ons.SortedList as well. It might be exactly
what you are after.

HTH
B\rgds
100

"Arjen" <bo*****@hotmai l.com> wrote in message
news:bt******** **@news4.tilbu1 .nb.home.nl...
Hello,

Let's say that we have a hashtable with some person objects.
This persons have a name.

Now I want to sort the people objects inside the hashtable by name.
How can the hashtable do this for me?

Thanks!

Nov 15 '05 #4
This might help:

http://msdn.microsoft.com/library/de...rp01212002.asp

--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights.
"100" <10*@100.com> wrote in message
news:OF******** ******@TK2MSFTN GP11.phx.gbl...
Hi Arjen,
As long as hashtables are not ordered collections you cannot sort them.
However, you can maintain a separate list along with the hashtable which
will keep the order of the elements and you can sort this list and use it
latter to retreive the elements.
Take a look on System.Collecti ons.SortedList as well. It might be exactly
what you are after.

HTH
B\rgds
100

"Arjen" <bo*****@hotmai l.com> wrote in message
news:bt******** **@news4.tilbu1 .nb.home.nl...
Hello,

Let's say that we have a hashtable with some person objects.
This persons have a name.

Now I want to sort the people objects inside the hashtable by name.
How can the hashtable do this for me?

Thanks!


Nov 15 '05 #5
Thanks!
"Eric Gunnerson [MS]" <er****@online. microsoft.com> schreef in bericht
news:uX******** ******@tk2msftn gp13.phx.gbl...
This might help:

http://msdn.microsoft.com/library/de...rp01212002.asp
--
Eric Gunnerson

Visit the C# product team at http://www.csharp.net
Eric's blog is at http://weblogs.asp.net/ericgu/

This posting is provided "AS IS" with no warranties, and confers no rights. "100" <10*@100.com> wrote in message
news:OF******** ******@TK2MSFTN GP11.phx.gbl...
Hi Arjen,
As long as hashtables are not ordered collections you cannot sort them.
However, you can maintain a separate list along with the hashtable which
will keep the order of the elements and you can sort this list and use it latter to retreive the elements.
Take a look on System.Collecti ons.SortedList as well. It might be exactly what you are after.

HTH
B\rgds
100

"Arjen" <bo*****@hotmai l.com> wrote in message
news:bt******** **@news4.tilbu1 .nb.home.nl...
Hello,

Let's say that we have a hashtable with some person objects.
This persons have a name.

Now I want to sort the people objects inside the hashtable by name.
How can the hashtable do this for me?

Thanks!



Nov 15 '05 #6

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

Similar topics

2
20317
by: Mark | last post by:
I'm using an enumerator to iterate through a HashTable that contains all numeric keys. I'd like to iterarate through the HashTable based on the ordered keys. Is there a quick way to do this? Thanks! -Mark IDictionaryEnumerator myEnumerator = myHashTable.GetEnumerator(); while ( myEnumerator.MoveNext() ) { //Please print ordered by Key ... Response.Write("<BR>" + myEnumerator.Key.ToString() + ", " + myEnumerator.Value.ToString())
9
2263
by: Arjen | last post by:
Hello, Persons is a hashtable which I convert to an array. Person aPerson = new Person; Persons.Values.CopyTo( aPerson, 0 ); Now I can access the person items like aPerson.name or aPerson.birthdate.
2
6061
by: RSB | last post by:
How do i sort a Hash Table.. Thanks RSB
3
1498
by: Alexander Widera | last post by:
Hi, I have a problem with this code ... (see below) ... I want to sort an instance of MyList ... by MyData.Shortname ... Shortname is of the type string.... how can I sort the entries? Thank you for your help. Alex using System;
2
5546
by: Ali | last post by:
I am binding a hashTable to a dropDownList to pick a State (key: like New York) and sends the state designation (value: NY) to a filtering procedure. I have entered the states in the hashTable in alphabetical order, but the dropDownList renders them in a non-order list. code snippet: Dim state as New hashTable state.Add("Alabama", "AL") state.Add("Alaska", "AK")
3
6199
by: Jim Adams | last post by:
I'm counting the frequency of word occurances and would like to return a key/value list sorted descending by frequency. So I need to quickly see if a word (key) is in the list, but later sort the list descending by values. Any ideas on how to do this efficiently? Thanks,
6
14673
by: max sharma | last post by:
Hi all, I am using a hashtable for my application. Its similar to word count application. How can I sort the hashtable w.r.t. the VALUE and not the KEY. The sample data of the table is given below which is sorted according the URL as in Dictionary: www.webroot.com (11) www.webshots.com (1) www.weddingprints.com (4) here, Key = URL (e.g. www.webroot.com)
11
4807
by: Cappamontana | last post by:
Hi, I'm stuck with a problem...in .Net C# I want to sort an ArrayList containing Hashtables. The sort key must be the key of the Hashtable. How can I do this? I've coded some example here... ArrayList arr = new ArrayList(); Hashtable row = new Hashtable(); Hashtable row2 = new Hashtable();
1
1310
by: Pedro Rosas Silva | last post by:
Hi all, I have a datagrid in an ASP.Net 1.1 web application which is binded to an HashTable. The grid has several columns and I want to sort the results by one of those columns. Are you aware of any way to do it? Usually, I perform these sorts by changing the SQL queries behind the datagrid.
0
10568
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.