Search code examples
kotlinkotlin-extension

Extension function of String class


I want to create an extension function of String which takes a String and returns a new String with the characters as the first, but sorted in ascending order. How can I do this? I am new to Kotlin.


Solution

  • fun String.ascending() = String(toCharArray().sortedArray())
    

    Then:

    println("14q36w25e".ascending())  // output "123456eqw"