I am trying to implement a Comparator, considering that the objects going to be compared (Word
) have two properties defined as int
.
I want to get the standard deviation from these two values (may be more, but 2 now), and sort my list by which objects have the lowest one. But apparently, it is saying that my method is not transitive based on this question (I presume, since I've got the same exception). But I cannot see how, here it will just compare the results of the standard deviation.
Am I confused with the math and did not consider a special case where points out that this method is not transitive or have I done something wrong?
Sorting the list:
for(Map.Entry<String,List<Word>> entry: list.entrySet()){
Collections.sort(entry.getValue(), Collections.reverseOrder(new SimpleComparator()));
...
}
Comparator Class:
import java.util.Comparator;
public class SimpleComparator implements Comparator<Word> {
@Override
public int compare(Word word1, Word word2) {
int b1,b2,f1,f2;
double average1,average2, result1,result2;
b1 = word1.getAttr1();
b2 = word2.getAttr1();
f1 = word1.getAttr2();
f2 = word2.getAttr2();
average1 = (b1-f1)/2;
average2 = (b2-f2)/2;
result1 = Math.sqrt((Math.pow(b1-average1,2)+Math.pow(f1-average1,2))/2);
result2 = Math.sqrt((Math.pow(b2-average2,2)+Math.pow(f2-average2,2))/2);
return (int)(result1 - result2);
}
}