We have used the sortedWith method. According to documentation, sortedWith does this:
Returns a sequence that yields elements of this sequence sorted according to the specified comparator.
Apart from that, we've made use of the kotlin.comparisons package, which provides us two main functions used in the preceding solution:
- public inline fun <T: Comparable<T>> nullsLast(): This method provides a comparator of nullable comparable values considering null value greater than any other value. That's how we can get the null items at the end, because they are considered bigger than any other values.
- compareBy(comparator: Comparator<in K>, crossinline selector: (T) -> K): This function accepts a comparator (such as nullsLast()) and ...