• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Hashtable

 
Harsha vaka
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harsha vaka
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic