“Comparison method violates its general contract!”

前端 未结 11 2047
梦如初夏
梦如初夏 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:47

    Just because this is what I got when I Googled this error, my problem was that I had

    if (value < other.value)
      return -1;
    else if (value >= other.value)
      return 1;
    else
      return 0;
    

    the value >= other.value should (obviously) actually be value > other.value so that you can actually return 0 with equal objects.

提交回复
热议问题