Reputation: 959
I am trying to sort a TreeMap (having Double as a value and Integer value as a key) using Comparator interface but it's not working.
// Create a tree map
TreeMap tm = new TreeMap();
// Put elements to the map
tm.put(1, new Double(3434.34));
tm.put(0, new Double(123.22));
tm.put(4, new Double(1378.00));
tm.put(2, new Double(99.22));
tm.put(3, new Double(-19.08));
List<Map.Entry> valueList = new ArrayList(tm.entrySet());
// Collections.sort(valueList, new Sort());
Collections.sort(valueList, new Sort());
HashMap sortedMap = new HashMap();
// Get an iterator
Iterator<Map.Entry> i = valueList.iterator();
// Display elements
while (i.hasNext()) {
Map.Entry object = i.next();
sortedMap.put(object.getKey(), object.getValue());
}
List sortedList = new ArrayList(sortedMap.entrySet());
Iterator<Map.Entry> iterator = sortedList.iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
System.out.println("Value " + entry.getValue() + "\n");
}
The following is my Comparator class
public class Sort implements Comparator<Map.Entry> {
@Override
public int compare(Map.Entry o1, Map.Entry o2) {
// TODO Auto-generated method stub
double valueOne = (Double) o1.getValue();
double valueTwo = (Double) o2.getValue();
int returnValue =
valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);
return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
}
}
But i am getting the following output
Value 123.22
Value 3434.34
Value 99.22
Value -19.08
Value 1378.0
Edited Part
public int compare(Map.Entry o1, Map.Entry o2) {
// TODO Auto-generated method stub
double valueOne = ((Double) o1.getValue()).doubleValue();
double valueTwo = ((Double) o2.getValue()).doubleValue();
int returnValue =
valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1);
return (valueOne > valueTwo ? -1 : (valueOne == valueTwo ? 0 : 1));
}
Upvotes: 2
Views: 334
Reputation: 43456
The Comparator
works only your keys, not entries. You need a Comparator<Integer>
, and pass in instance of this Comparator to the TreeMap
constructor.
TreeMap<Integer,Double> tm = new TreeMap<Integer,Double>(myIntegerComparator);
In your example, the behavior you see is due to the TreeMap
using Integer
's standard comparsion (this works because Integer
is Comparable<Integer>
).
(Btw, you should also read up on generics and use them in your collections classes, as well as any other parameterized class.)
Upvotes: 1
Reputation: 12538
Adding to what others have already suggested, try this:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class SortDemo
{
public class Sort implements Comparator<Map.Entry>
{
public int compare(Entry o1, Entry o2)
{
Double valueOne = (Double) o1.getValue();
Double valueTwo = (Double) o2.getValue();
return (int) Math.signum(valueOne.compareTo(valueTwo));
}
}
public static void main(String[] args)
{
new SortDemo().foo();
}
void foo()
{
TreeMap tm = new TreeMap();
tm.put(1, new Double(3434.34));
tm.put(0, new Double(123.22));
tm.put(4, new Double(1378.00));
tm.put(2, new Double(99.22));
tm.put(3, new Double(-19.08));
List<Map.Entry> valueList = new ArrayList(tm.entrySet());
Collections.sort(valueList, new Sort());
Iterator<Map.Entry> iterator = valueList.iterator();
while (iterator.hasNext())
{
Map.Entry entry = iterator.next();
System.out.println("Value: " + entry.getValue());
}
}
}
Upvotes: 1
Reputation: 4164
Using a HashMap for sorting will not work because its not a list. You need to look into an ArrayList and the sort method.
ArrayList<double> arr = new ArrayList<double>();
sort(arr) //sort ascending
Upvotes: 0
Reputation: 8386
When you put them into the HashMap
they will not retain their order, as a HashMap
is not an ordered map.
Upvotes: 1
Reputation: 888213
HashMap
is intrinsically unordered.
Instead, you can create a TreeMap
with your comparator in the first place.
Upvotes: 5