How to sort characters in a string e.g. "5121" -> "1125" ?
I can do this with code below but it seems too slow:
var nonSortedString = "5121"
var sortedString = String(Array(nonSortedString.characters).sort())
How to sort characters in a string e.g. "5121" -> "1125" ?
I can do this with code below but it seems too slow:
var nonSortedString = "5121"
var sortedString = String(Array(nonSortedString.characters).sort())
The CharacterView
handles properly compound characters and provides proper ordering ("eěf" vs. "efě"). If you are ok with the way C++ handles unicode characters, try using one of the other views, like nonSortedString.utf16.sort()
. It should give speed similar to C++.