简体   繁体   English

快速排序字符串中的字符

[英]swift sort characters in a string

How to sort characters in a string eg "5121" -> "1125" ? 如何对字符串中的字符进行排序,例如“ 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ě"). CharacterView处理正确的复合字符并提供适当的顺序(“eěf”与“efě”)。 If you are ok with the way C++ handles unicode characters, try using one of the other views, like nonSortedString.utf16.sort() . 如果您对C ++处理Unicode字符的方式nonSortedString.utf16.sort() ,请尝试使用其他视图之一,例如nonSortedString.utf16.sort() It should give speed similar to C++. 它应该提供类似于C ++的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM