Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
sorted¶
If elements in a List implements the Comparable interface,
you can call the method sorted to return a sorted List (in nature order).
Note: Pair in Kotin does not implement the Comparable interface!!!
listOf(5, 9, 3, 2).sorted()[2, 3, 5, 9]val listPairs = listOf(Pair(5, 7), Pair(5, 3), Pair(1, 2), Pair(2, 3), Pair(1, 9))
listPairs[(5, 7), (5, 3), (1, 2), (2, 3), (1, 9)]listPairs.sorted()error: type parameter bound for T in fun <T : Comparable<T>> Iterable<T>.sorted(): List<T>
is not satisfied: inferred type Pair<Int, Int> is not a subtype of Comparable<Pair<Int, Int>>
listPairs.sorted()
^sortedBy¶
listPairs.sortedBy{ it.first }[(1, 2), (1, 9), (2, 3), (5, 7), (5, 3)]sortedWith¶
listPairs.sortedWith(compareBy({it.first}, {it.second}))[(1, 2), (1, 9), (2, 3), (5, 3), (5, 7)]References¶
https://
https://
https://
https://