• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:

Sort a Hashtable

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
bvdm23
Asked:
bvdm23
  • 3
  • 2
2 Solutions
 
TimYatesCommented:
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
 
TimYatesCommented:
>  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
 
KuldeepchaturvediCommented:
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!

 
KuldeepchaturvediCommented:
Why I take so much time in typing?????????
0
 
TimYatesCommented:
Hehehehe :-)
0
 
objectsCommented:
Try a TreeMap with a custom Comparator that compares the mapped values.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now