Solved

Sort a Hashtable

Posted on 2003-12-03
8
415 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
authenticate preemptively soap ui option 3 155
java cigar challenge 58 214
countXY challenge 28 160
instanceof  operator in java 26 95
IT certifications are a concrete representation of continual learning on the part of the candidate.  Continual learning is necessary for the long term success of an IT professional, but are IT certifications the right path for you?
The business world is becoming increasingly integrated with tech. It’s not just for a select few anymore — but what about if you have a small business? It may be easier than you think to integrate technology into your small business, and it’s likely…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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