swift sort characters in a string

别来无恙 提交于 2019-12-11 13:57:02

问题


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())

回答1:


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++.



来源:https://stackoverflow.com/questions/31904356/swift-sort-characters-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!