How do I access String.CASE_INSENSITIVE_ORDER from Kotlin?

You make 50% of work :-)

In kotlin String != java String. So it is right to call java.lang.String.CASE_INSENSITIVE_ORDER

Kotlin think that all things came from java are Nullable.

val CASE_INSENSITIVE_ORDER : Comparator<String?>

But we can fix it with external annotations. Go to definition of CASE_INSENSITIVE_ORDER, and do this http://blog.jetbrains.com/kotlin/using-external-annotations/ In our case we need to set it:

val CASE_INSENSITIVE_ORDER : Comparator<String>

Working sample

val map = hashMap<String, String>(“2” to “2”, “1” to “1”, “3” to “3”)

println(map) //{3=3, 2=2, 1=1}
val sorted = map.toSortedMap(java.lang.String.CASE_INSENSITIVE_ORDER)
println(sorted) //{1=1, 2=2, 3=3}

1 Like