Solved

Sort a Hashtable

Posted on 2003-12-03
8
408 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
Comment Utility
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
Comment Utility
>  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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
Comment Utility
Why I take so much time in typing?????????
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Hehehehe :-)
0
 
LVL 92

Expert Comment

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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now