Why is my simple comparator broken?

前端 未结 6 1267
故里飘歌
故里飘歌 2021-01-04 01:56

I have a class, which I have simplified to this:

final class Thing {
    private final int value;
    public Thing(int value) {
        this.value = value;
          


        
6条回答
  •  猫巷女王i
    2021-01-04 02:14

    When comparing Java primitives, it is advisable to convert them to their Object counterparts and rely on their compareTo() methods.

    In this case you can do:

    return Integer.valueOf(a.getValue()).compareTo(b.getValue())
    

    When in doubt, use a well-tested library.

提交回复
热议问题