什麼是Kotlin通過空值字段排序對象列表的方式,最後是空值?Kotlin排序空值最後
科特林對象進行排序:
@JsonInclude(NON_NULL)
data class SomeObject(
val nullableField: String?
)
模擬下面的Java代碼:
@Test
public void name() {
List<SomeObject> sorted = Stream.of(new SomeObject("bbb"), new SomeObject(null), new SomeObject("aaa"))
.sorted(Comparator.comparing(SomeObject::getNullableField, Comparator.nullsLast(Comparator.naturalOrder())))
.collect(toList());
assertEquals("aaa", sorted.get(0).getNullableField());
assertNull(sorted.get(2).getNullableField());
}
@Getter
@AllArgsConstructor
private static class SomeObject {
private String nullableField;
}