Background: I am new to kotlin and has some experience in java.
Problem:
I am going through the kotlin intellij tutorial to learn about collections and the way to use the sort* functions but I am confused about the syntax.
The code written to use the kotlin sortedByDescending function :
// Return a list of customers, sorted in the descending by number of orders they have made
fun Shop.getCustomersSortedByOrders(): List<Customer> =
customers.sortedByDescending {
it.orders.size // I am confused here
}
the definition of kotlin sortedByDescending function
public inline fun <T, R : Comparable<R>> Iterable<T>.sortedByDescending(crossinline selector: (T) -> R?): List<T> {
return sortedWith(compareByDescending(selector))
}
To my understanding, the sortedByDescending takes a function which input type is T and returns an output in type Comparable<R>> Iterable<T>.
- what does the return type mean? it looks like
Rneeds to implement theComparablebut also needs to be anIteratableofT. Is my understanding correct? - if my understanding is right, the how does
it.orders.sizefit into that type? I knowitin kotlin is used for single params in lambda