comparator with null values

前端 未结 9 1168
萌比男神i
萌比男神i 2020-12-04 16:25

We have some code which sorts a list of addresses based on the distance between their coordinates. this is done through collections.sort with a custom comparator.

How

9条回答
  •  臣服心动
    2020-12-04 17:04

    Handle it like null means infinitely far away. Thus:

    • comp(1234, null) == -1
    • comp(null, null) == 0
    • comp(null, 1234) == 1

    With this, you get a consistent ordering.

提交回复
热议问题