Solved

Sort a Hashtable

Posted on 2003-12-03
8
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

726 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