473,406 Members | 2,369 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,406 software developers and data experts.

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 7700
Arjen <bo*****@hotmail.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.Values);
al.Sort (myNameComparator);

--
Jon Skeet - <sk***@pobox.com>
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.com> wrote:
Arjen <bo*****@hotmail.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.Values);
al.Sort (myNameComparator);


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.Collections.SortedList as well. It might be exactly
what you are after.

HTH
B\rgds
100

"Arjen" <bo*****@hotmail.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**************@TK2MSFTNGP11.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.Collections.SortedList as well. It might be exactly
what you are after.

HTH
B\rgds
100

"Arjen" <bo*****@hotmail.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**************@tk2msftngp13.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**************@TK2MSFTNGP11.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.Collections.SortedList as well. It might be exactly what you are after.

HTH
B\rgds
100

"Arjen" <bo*****@hotmail.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
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?...
9
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...
2
by: RSB | last post by:
How do i sort a Hash Table.. Thanks RSB
3
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...
2
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...
3
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...
6
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...
11
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... ...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.