0

Maybe I'm missing something here, but it seems like sort isn't sorting the array completely.

This is what I tried:

$ kotlinc
Welcome to Kotlin version 1.2.71 (JRE 10.0.2+13-Ubuntu-1ubuntu0.18.04.2)
Type :help for help, :quit for quit
>>> var test: String = "This is a test of the user system."
>>> var testarray2: Array<String> = test.split(" ").toTypedArray()
>>> testarray2.sort()
>>> testarray2.forEach { println("${it} ") }
This 
a 
is 
of 
system. 
test 
the 
user 
>>> 

As you can see, sorting doesn't seem to be sorting the string "This."

I tried installing openjdk-11 and updating all my packages, but I'm still getting this, and I checked that 1.2.71 is the latest Kotlin package.

Am I doing something wrong?

Thank you in advance.

1 Answer 1

2

if you do the sorting like this:

testarray2.sortBy { it.toLowerCase() }

it will be sorted as you wanted it.
The sort() method compares the strings using the ASCII codes of the characters and all uppercase characters have ASCII codes less than the lower case ones so you got the result that seemed to you wrong.
On the other hand it.toLowerCase() will transform all strings to lower case before the sorting so you have case insensitive sorting.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea I just realized that. That was a dumb waste of time. Thank you for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.