W3cubDocs

/Kotlin

sortedMapOf

Platform and version requirements: JVM (1.0)
fun <K : Comparable<K>, V> sortedMapOf(
    vararg pairs: Pair<K, V>
): SortedMap<K, V>

Returns a new SortedMap with the specified contents, given as a list of pairs where the first value is the key and the second is the value.

The resulting SortedMap determines the equality and order of keys according to their natural sorting order.

import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val map = sortedMapOf(Pair("c", 3), Pair("b", 2), Pair("d", 1))
println(map.keys) // [b, c, d]
println(map.values) // [2, 3, 1]
//sampleEnd
}
Platform and version requirements: JVM (1.4)
fun <K, V> sortedMapOf(
    comparator: Comparator<in K>, 
    vararg pairs: Pair<K, V>
): SortedMap<K, V>

Returns a new SortedMap with the specified contents, given as a list of pairs where the first value is the key and the second is the value.

The resulting SortedMap determines the equality and order of keys according to the sorting order provided by the given comparator.

import kotlin.test.*
import java.util.*

fun main(args: Array<String>) {
//sampleStart
val map = sortedMapOf(compareBy<String> { it.length }.thenBy { it }, Pair("abc", 1), Pair("c", 3), Pair("bd", 4), Pair("bc", 2))
println(map.keys) // [c, bc, bd, abc]
println(map.values) // [3, 2, 4, 1]
//sampleEnd
}

© 2010–2020 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/sorted-map-of.html