aspose file tools*
The moose likes Java in General and the fly likes Sorting Hashtable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting Hashtable" Watch "Sorting Hashtable" New topic
Author

Sorting Hashtable

Harsha vaka
Greenhorn

Joined: Oct 03, 2007
Posts: 4
Hello All

I need to sort hash table based on values (not key),and there will be duplicate values. Let me explain with an example dataset

KEY VALUE
----------------
ABD 12.34
DCVB 34.56
EFC 12.34

I have above mentioned dataset, and i need to sort it based on "VALUE" and output shouls look like this

OUTPUT
-----------
ABD 12.34
EFC 12.34
DCVB 34.56


ANYONE PLEASE HELP ME, I AM WORKING ON IT FOR A LONG TIME USING HASHTABLE BUT ITS DELETING DUPLICATE "VALUES" BUT I NEED TO PERSIST THOSE DUPLICATE VALUES ALSO.
PLEASE HELP ME ASAP.

THANKS IN ADVANCE.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

HashTables are an un-ordered collection. You can't sort them. What you can do is take values out or take the Map.Entries and put them into an Ordered collection, like a TreeSet that you could then sort with an appropriate Comparator.


Steve
Harsha vaka
Greenhorn

Joined: Oct 03, 2007
Posts: 4
Hey Steve

Could you be more detail i cant get it, is my desired output is possible at all? I REALLY APPRECIATE YOUR HELP.

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
If you post outside beginner's, people will assume you already know the details, or how to find them. And please don't WRITE ALL UPPER-CASE.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Using Steve's solution:

step 1: create a Comparator:

step 2: put all entries in a TreeSet along with this compatator. Map.entrySet() will help you out.

Don't forget to check the key in sorting if the values are the same, or some entries will not show up.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You can't sort inside the HashTable, so you have to sort outside the table using some sortable collection. You could use a TreeMap which would be analogous to a sorted HashTable, but TreeMaps (and all Maps that I am aware of) sort on the Key, not the Value. So Maps are out.

Your options are then Sets or Lists, and Sets tend to be a better fit to Map data... You have a TreeSet which will provide the Sorting you need, as long as you provide it a Comparator.

So now you need to know what data actually needs to be sorted - the simplest would be to just sort the Values, which you conveniently get out of the HashTable using table.values(). You would then addAll() to the TreeSet and your done.

If you need the Keys to go along for the ride with the sorting then you would need to get the Set of Map.Entrys from the HashTable, easily done using the table.entrySet() method, the results of which you could again addAll() to a TreeSet and be done.

Your key will be to provide a suitable Comparator which knows how to compare the values you enter.
[ October 11, 2008: Message edited by: Steve Luke ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sorting Hashtable