Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Create an Empty List

[]
[]

Create a List with Initial Values

[0, 1, 2, 3, 4]

Concatenate List with Other Iterables

[0, 1, 2, 3, 4, 5]
[0, 1, 2, 3, 4, 5]

Sort a List

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!!!

[2, 3, 5, 9]
[(5, 7), (5, 3), (1, 2), (2, 3), (1, 9)]
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

[(1, 2), (1, 9), (2, 3), (5, 7), (5, 3)]

sortedWith

[(1, 2), (1, 9), (2, 3), (5, 3), (5, 7)]

Methods and Operators

1, 2, 3
[1, 2]
[0, 1, 2]
[3, 4]

data class is preferred over Tuple-like data.

3.0