0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Kotlin KoansでKotlin入門 第20回:Sort

Posted at

はじめに

公式の問題集「Kotlin Koans」を解きながらKotlinを学習します。

過去記事はこちら

問題

Sort

顧客のリストを、彼らが行った注文の数によって降順にソートして返す関数を実装します。
sortedDescending または sortedByDescending を使用します。

val strings = listOf("bbb", "a", "cc")
strings.sorted() ==
        listOf("a", "bbb", "cc")

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

strings.sortedDescending() ==
        listOf("cc", "bbb", "a")

strings.sortedByDescending { it.length } ==
        listOf("bbb", "cc", "a")

修正前のコード

// 顧客のリストを、注文数の降順でソートして返す
fun Shop.getCustomersSortedByOrders(): List<Customer> =
        TODO()

問題のポイント

コレクションの順序と、変更可能なコレクションに対する操作と新しいコレクションを返す操作の違いについて学びます。

解答例

fun Shop.getCustomersSortedByOrders(): List<Customer> = customers.sortedByDescending { it.orders.size }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?