Solved

Sort a Hashtable

Posted on 2003-12-03
8
416 Views
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
key.

Can anybody help me with this?

Thanks in advance,

bvdm23
0
Comment
Question by:bvdm23
  • 3
  • 2
8 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 75 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 :-)

http://www.airporttools.com/other/programming/hashsorting/
0
 
LVL 35

Expert Comment

by:TimYates
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 :-)

Tim
0
 
LVL 19

Assisted Solution

by:Kuldeepchaturvedi
Kuldeepchaturvedi earned 75 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());
al=Java.util.Collections.sort(al);
 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)..
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

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

Expert Comment

by:TimYates
ID: 9865551
Hehehehe :-)
0
 
LVL 92

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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

Suggested Solutions

While it may be true that the internet is a place of possibilities, it is also a hostile environment lurking with many dangers. By clicking on the wrong link, trusting the wrong person or using a weak password, you are virtually inviting hackers to …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

756 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