Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bubble Sort Set using Comparator

 
Prabha Dhandapani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have a Set with the follwing key/values:



I need to bubble sort the above Set using Comparator and the result should be:



My code is as follows and I am noe sure what I am mising here...I am getting a ClassCastException




Would appreciate very much if ranchers can rescue me as this is very urgent .

Many Thx in Advance,
Prabha
 
Ian Darwin
author
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prabha Dhandapani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response!

But I have a need to seperately show the Key and Values.

So Here is my code and it is not working...I am getting NullPointerException.Would appreciate if somebody can give me a good solution.


The result that I am expecting is:

Key Value
A 1
D 1
K 3
S 10

Note:Values are not unique and I need to sort it based on Values only.

1)I am putting all the key/Value pairs into TreeMap tree.
2)Passing this tree to TreeSort
3)TreeSort sorts it and I put the sorted result into another TreeMap called sortedMap.

I dont know if this is the right way to do it...Would appreciate very much if somebody can help me.This is very very urgent.

Many Thx in advance,
Prabha
 
Prabha Dhandapani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry I missed some lines...


The result that I am expecting is:

Key Value
A 1
D 1
K 3
S 10

Note:Values are not unique and I need to sort it based on Values only.

1)I am putting all the key/Value pairs into TreeMap tree.
2)Passing this tree to TreeSort
3)TreeSort sorts it and I put the sorted result into another TreeMap called sortedMap.

I dont know if this is the right way to do it...Would appreciate very much if somebody can help me.This is very very urgent.

Many Thx in advance,
Prabha
 
Prabha Dhandapani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once I get the above TreeMap sorted, I need to bubble sort it which I dont Know how to do for a TreeMap.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic