Let’s break down how to sort collections in Kotlin when the key can be
null: why sorting needs an explicit contract (“
null first/last”), how to use the Elvis operator
?: with safe defaults (
sortedBy,
sortedByDescending), why “magic numbers” like
Int
.
MAX_VALUE can be dangerous, and when it’s better to split the data (
filterNotNull(), counting
null, split+merge). Everything is shown using an expenses report example in a hypothetical BudgetBuddy app.