Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i use joined() function after using sorted() in swift?

Tags:

sorting

swift

let var1 = "AnyCode".sorted()
print(var1.joined(separator:""))

ERROR: No exact matches in call to instance method 'joined'

I am trying to join the array after sorting the string. = "AnyCode"

I was expecting the output was = ACdenoy

But it is giving an error.

like image 511
Sumit Chandora Avatar asked Jan 30 '26 00:01

Sumit Chandora


1 Answers

A Swift String is a collection of Characters, and sorted() applied to a collection returns an array with the collection elements in sorted order.

So var1 has the type [Character], and you can simply create a new string from that array with:

let var1 = "AnyCode".sorted()
print(String(var1)) // ACdenoy
like image 89
Martin R Avatar answered Feb 01 '26 14:02

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!