Sort a Hashtable

Posted on 2003-12-03
Medium Priority
Last Modified: 2010-04-01
Hello all,

I have a Hashtable with as a key a String and as a value a String.
Now I want to sort this HashTable by the value while still be able to retreive the

Can anybody help me with this?

Thanks in advance,

Question by:bvdm23
  • 3
  • 2
LVL 35

Accepted Solution

TimYates earned 300 total points
ID: 9865500
You can't sort hashtables, as they have no inherrant order

This page explains how to extract your hashtable to an arraylist or vector, then sort that :-)

LVL 35

Expert Comment

ID: 9865506
>  as they have no inherrant order

By this I mean that you can think of Hashtables, Sets, etc as being like a bag of sweets...  To sort them (in order of colour or size), you will have to put them in a struture that can hold order (like a queue, or list) :-)

Hope that makes more sense :-)

LVL 19

Assisted Solution

Kuldeepchaturvedi earned 300 total points
ID: 9865541
Well, I don't see any reason to sort the hashtable.. but here is what you can do..

ArrayList al=Java.util.Collections.list( ht.elements());
 This should give you sorted values inside this arraylist..
Obviously your Hashmap will not get sorted inside ( and it should not be anyways... because hashmap decides how he wants to stores the values for effiecient fetch)..
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 19

Expert Comment

ID: 9865548
Why I take so much time in typing?????????
LVL 35

Expert Comment

ID: 9865551
Hehehehe :-)
LVL 92

Expert Comment

ID: 9872591
Try a TreeMap with a custom Comparator that compares the mapped values.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The article describes step by step how to validate the SRA software certificate once installed on the Site Recovery Manager server. I hope it will be very useful for all administrators of virtualized environments.
With the emergence of Office 365 as a superior email communication platform, many organizations have started switching over to it.  After migrating to Office 365, sometimes users, as well as organizations, will have to import PST files to Office 36…
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…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question