快速排序字符串中的字符 [英] swift sort characters in a string

查看:107
本文介绍了快速排序字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对字符串中的字符进行排序,例如 5121-> 1125?

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


推荐答案

CharacterView 可以正确处理复合字符并提供正确的顺序( eěf与efě)。如果您对C ++处理Unicode字符的方式还满意,请尝试使用其他视图之一,例如 nonSortedString.utf16.sort()。它应该提供类似于C ++的速度。

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

这篇关于快速排序字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆