There are several issues here.
I have a Set with the following key/values:
No, you don't :-). Set is like List, it only contains values, not keys/values. Only Map contains keys/values. As well, the values you show as input are not the same as those you show for output.
Set ans = allAnswers.entrySet(); //allAnswers is TreeSet
//Collections.sort( ans , new TreeSort()); //I am getting a ClassCastException here
Actually, the code as shown won't compile if you uncomment it, since Collections.sort() takes a List, not a Set. List and Set are both sub-interfaces of Collection, but they are not interchangeable.
the TreeSort class is incomplete and I dont know how to finish it to swap the values...
A Comparator does not swap values, it simply compares them. Collections.sort or some other client does the swapping.
Note also that TreeMap, an implementation of SortedMap, will keep things in order for you.
You must never try to sort a SortedMap. The problem is that SortedMap keeps things in order by the key values, not the value values.
I'm assuming that the complexity is because you want them sorted by value, not by key as SortedMap does. Otherwise you could just use a SortedMap. Here's an off-the-top-of-my-head, late-at-night implementation that sorts Map-like entries by value. Extra points to somebody who rewrites it to not use an inner class, or better uses the existing API to the same effect.
Does my version sort by values correctly? I ran it and got this:
If you still have questions, please read Chapter 7 before you post them :-)
Cheers
Ian