Kotlin Koans 25 Sort

Sort (Playground)

Description

Implement Shop.getCustomersSortedByNumberOfOrders() using sorted or sortedBy.

sorted もしくは sortedBy を使用して、Shop.getCustomersSortedByNumberOfOrders() を実装してください。

listOf("bbb", "a", "cc").sorted() == listOf("a", "bbb", "cc")
listOf("bbb", "a", "cc").sortedBy { it.length } == listOf("a", "cc", "bbb")

Code

// Return a list of customers, sorted by the ascending number of orders they made
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> = customers.sortedBy { it.orders.size }

← Blog