There are a couple subtle things going on here. This is not the usual "broken comparator" problem seen elsewhere on Stack Overflow. Although this comparator is indeed broken, it's pretty hard to see.
The first issue is that the comparator is essentially responsible for comparing Double values, that is, boxed double values. Whereas the > operator will perform automatic unboxing and do a numeric comparison of the contained values, the == operator will test reference equality. In general,
Double.valueOf(1.23) == Double.valueOf(1.23) // WARNING: reference comparison, not numeric!
will be false. If you really wanted to test numeric equality of Double values, you'd have to do
if (o1.getValue().doubleValue() == o2.getValue.doubleValue()) ...
This would mostly work if your input contains only actual numeric values. I suspect, though, that your input contains NaN values, which have obscure (even nonsensical) behavior. In particular, comparing NaN to any numeric value is false, and NaN compares not equal to itself! This violates all sorts rules about numeric comparisons; indeed, NaN is unordered with respect to real numbers. This is why the sort algorithm breaks when it encounters NaN values.
(NaN is the result of dividing 0.0 by 0.0, among other things.)
There is a method Double.compare(double d1, double d2) that handles NaN reasonably; it sorts NaN values above Double.POSITIVE_INFINITY. (It also distinguishes positive zero from negative zero, but that's unlikely to have caused your problem.) There is a companion method Double.compareTo(Double) that compares boxed Double values.
I'd rewrite your comparator like this:
Collections.sort(res, new Comparator<Entry<Integer, Double>>() {
public int compare(Entry<Integer, Double> o1,
Entry<Integer, Double> o2) {
if (isDesc) {
return o2.getValue().compareTo(o1);
} else {
return o1.getValue().compareTo(o2);
}
}
}
Since Double is Comparable itself, in Java 8 you can avoid writing your own comparator by using a utility method on Map.Entry. You can also use the sort() default method on List which will be faster in general:
if (isDesc) {
res.sort(Map.Entry.<Integer,Double>comparingByValue().reversed());
} else {
res.sort(Map.Entry.comparingByValue());
}
(Unfortunately, the type inference doesn't quite work, so you have to provide a "type witness" in order to get the reversed comparator.)
Finally, you can use the "diamond" operator and the ArrayList copy constructor to copy the map entries much more concisely. The rewritten routine looks like this:
public static List<Entry<Integer, Double>> sortMap(
Map<Integer, Double> curMap, final boolean isDesc) {
List<Entry<Integer, Double>> res = new ArrayList<>(curMap.entrySet());
if (isDesc) {
res.sort(Map.Entry.<Integer,Double>comparingByValue().reversed());
} else {
res.sort(Map.Entry.comparingByValue());
}
return res;
}