Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a string array in kotlin

Tags:

kotlin

How to sort the following string array in kotlin in alphabetic order?

val array = arrayOf("abc","bcd","xyz","ghi","acd")
like image 411
Thameem Abdul Latheef C Avatar asked Dec 03 '22 12:12

Thameem Abdul Latheef C


1 Answers

To sort the same array we can use

array.sort()

This inbuilt method will sort in alphabetic order. We can also sort Int Array and other array types using inbuilt sort() method

To sort an array without changing the original we can use

val array = arrayOf("abc","bcd","xyz","ghi","acd")
val sorted = array.sortedArray()

as mentioned above answer by s1m0nw1

like image 147
Thameem Abdul Latheef C Avatar answered Dec 11 '22 15:12

Thameem Abdul Latheef C