Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Sorting Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting Problem" Watch "Sorting Problem" New topic
Author

Sorting Problem

Anirban dutta
Ranch Hand

Joined: Aug 08, 2002
Posts: 89
In a certain programming problem, I have 2 things:
1)A methods name.
2)It's parameters as Class objects arrays.
I have a HashTable containing this 2 with the keys as the Parameters array object, and the corresponding value as the corresponding method's name. I want to sort this set of data based on the method names. Please help me on how to do this. Is it possible to do through some collection class or it can be done through pure programming logic. The logic which I have currently applied is as follows :
I take the Collection of the values in the HashTable, convert it in an array, then sort it. Then I take each element from the array currently formed, iterate through the HashMap, find the corresponding key, and then put this key-value pair in a new HashTable. Thus the new HashTable contains the sorted values. I know this solution is a terrific one on part of efficiency, but I don't have any other solution. Please help me out
to have a better solution.
Many,many thanks in advance
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
What you'll probably want to do is extract the entrySet() from your Map, then sort them with a custom ComparatorYou can sort by turning the Set into a List and sorting that - Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
I'm a bit mystified though by your reference to the final HashTable (you mean HashMap, I hope) containing sorted values. Hash-anything doesn't preserve sorting order.
- Peter
Anirban dutta
Ranch Hand

Joined: Aug 08, 2002
Posts: 89
Dear peter,
Yah you pointed it out right, I referenced everywhere HashMap as HashTable. I am very sorry for that. I find the solution given by you to be an elegant one. Will you please elborate on how to use a custom Comparator.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Your can learn more about Comparator through the Java Tutorial, or most good basic Java texts. In Peter's code, the entrySet() method returns a Set of Map.Entry objects. Each of these objects cointains a key and a value; you can use getKey() and getValue() to access them. Peter is assuming you really do need to sort things by value (rather than key), so his Comparator uses getValue to access the values of two different Map.Entry objects and compare them.
I agree with Peter that the idea of ordering a HashMap is... odd. Hashtable and HashMap have no order really (none worth trying to understand anyway). If you want a Map which is sorted based on the order of the keys, you could use a TreeMap. If you really want something sorted by value, I suppose you could use a LinkedHashMap. After sorting the ArrayList containing all the entries, put them in a LinkedHashMap:

Now any time you iterate through the entrySet(), keySet(), or values() of this map, elements will be found in the order they were inserted. If you add any new elements to the map later, they will occur at the end - probably you would need to re-sort all the entries again and put them in a new LinkedHashMap. (Or remove them all from the old map and then re-insert them in the new order.)


"I'm not back." - Bill Harding, Twister
 
GeeCON Prague 2014
 
subject: Sorting Problem