jegadees waran wrote:I am trying out a solution to sort a tree map based on its values...
First off - you need to get a few pieces of terminology straight:
1. You don't 'sort' a
TreeMap because it's already "sorted" (indeed, it implements
SortedMap (←click)). That is to say: it already
has an order. What you
might need to do is order it
differently (but I suspect not).
2. Don't confuse the words 'sort' and 'order'. 'Sort' is an
action - usually done by a method; 'Order' is implicit - ie, it says how you want the items (in your case, it would appear, lists) TO BE sorted.
Comparator implements an
order; it does NOT sort things.
The implication of the first point is that ALL TreeMaps have an order - even your "unsorted" one. This in turn means that either:
1. The items being stored are
Comparable (java.lang.Comparable), or:
2. You supply a
Comparator when you create the TreeMap.
If one of those two things isn't true, then you will get errors.
Second: We (or I) am still not sure
why you want to do this. If we did, we might be able to advise you better.
However, just off the top of my head:
1. A TreeMap called "unsorted" just doesn't make any sense.
2. Your declaration is for a
TreeMap<String, List<Bean>>, but I still have no idea what the "String" part is supposed to contain. If it's just a list of all the values in your List, then it's completely redundant and simply a waste of space; and I think you'd be much better off with a
TreeSet<List<Bean>>.
However, it all comes back to not knowing what you're trying to do, so if you could
TellTheDetails (←click), I think we'd be a lot better off.
HIH
Winston