dcsimg
Sort a hashtable on the basis of value its containing
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, January 13, 2005 05:44 AM

&I am having a hashset it contains object.In that object their is a Count variable on which we can make calculations.we can get the value of count by classname.getCount method.So now i want to sort that hashtable on the basis of value of the count

Sort a hashtable on the basis of value its containing

Posted By:   Anonymous  
Posted On:   Saturday, February 26, 2005 07:22 AM

I think use use TreeSet instead of hashtable for your prolem. Because TreeSet having natural sort(ordering) ability.

Re: Sort a hashtable on the basis of value its containing

Posted By:   Jani_Kaarela  
Posted On:   Thursday, January 13, 2005 06:05 AM


I am having a hashset it contains object.In that object their is a Count variable on which we can make calculations.we can get the value of count by classname.getCount method.So now i want to sort that hashtable on the basis of value of the count


I haven't got much experience on this particular kind of computing problem, but I think I know a suitable solution...



Instead of a HashSet, use TreeSet. By default, it uses "natural ordering", ie. as ordered using Comparable. It has a constructor which accepts a Comparator. Implement a Comparator that compares your objects' count value and pass it to the constructor.



You contradict yourself in your problem description: first, you say you're using a HashSet, then later you say it's a Hashtable. Which one is it? The above solution only applies to a SortedSet, since a SortedMap is sorted by it's keys, not values!

About | Sitemap | Contact