“Comparison method violates its general contract!”

前端 未结 11 2045
梦如初夏
梦如初夏 2020-11-21 06:07

Can someone explain me in simple terms, why does this code throw an exception, \"Comparison method violates its general contract!\", and how do I fix it?

pri         


        
11条回答
  •  情深已故
    2020-11-21 06:51

    In our case were were getting this error because we had accidentally flipped the order of comparison of s1 and s2. So watch out for that. It was obviously way more complicated than the following but this is an illustration:

    s1 == s2   
        return 0;
    s2 > s1 
        return 1;
    s1 < s2 
        return -1;
    

提交回复
热议问题