I have an ImmutableSortedMap that holds data in the following datastructure.
<String, Pair<Long, Double>>
My goal is to sort this map in descending order starting with the largest Double and if two Doubles are the same, sort it by the Long. If both the Double and Long are the same, sort it by String
My current comparator only deals with the first case, and looks like this.
private ImmutableSortedMap<String, Pair<Long, Double>> sortInDescendingSizeOrder(final Map<String, Pair<Long, Double>> statsByType) {
Ordering<String> ordering = Ordering
.from(new Comparator<Pair<Long, Double>>() {
@Override
public int compare(Pair<Long, Double> o, Pair<Long, Double> o2) {
return o.getRight().equals(o2.getRight()) ? o.getLeft().compareTo(o2.getLeft()) : o.getRight().compareTo(o2.getRight());
}
})
.reverse()
.onResultOf(Functions.forMap(statsByType, null));
return ImmutableSortedMap.copyOf(statsByType, ordering);
}
However, it finds two Doubles that are the same and throws the following exception: Exception in thread "main" java.lang.IllegalArgumentException: Duplicate keys in mappings
I don't understand what am I doing wrong...
EDIT: I have tried to add the compound method and order by String (at least).
.compound(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
This makes the code not throw any Exception, however, there is no ordering whatsoever in the resulted map.
EDIT 2: At this point, any ordering like the one described above of a
Map<String, Pair<Long, Double>>
will do. Doesn't necessarily need to be an ImmutableSortedMap