File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Bubble Sort Set using Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Bubble Sort Set using Comparator" Watch "Bubble Sort Set using Comparator" New topic
Author

Bubble Sort Set using Comparator

Prabha Dhandapani
Ranch Hand

Joined: Sep 11, 2003
Posts: 70
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

Joined: Aug 03, 2001
Posts: 64
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


Ian Darwin
Many questions are answered in my Java Cookbook, 2nd Edition
Prabha Dhandapani
Ranch Hand

Joined: Sep 11, 2003
Posts: 70
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

Joined: Sep 11, 2003
Posts: 70
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

Joined: Sep 11, 2003
Posts: 70
Once I get the above TreeMap sorted, I need to bubble sort it which I dont Know how to do for a TreeMap.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Bubble Sort Set using Comparator
 
Similar Threads
Using a Comparator in Collections
comparator
Doubt in collection Framework
TreeMap and Sorting
how comparator works