ios 如何将 NSMutableArray 中的字符串按字母顺序排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6315323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I sort strings in NSMutableArray into alphabetical order?
提问by praveena
I have a list of strings in an NSMutableArray
, and I want to sort them into alphabetical order before displaying them in my table view.
我在 中有一个字符串列表NSMutableArray
,我想在将它们显示在我的表视图中之前按字母顺序对它们进行排序。
How can I do that?
我怎样才能做到这一点?
回答by Vijay-Apple-Dev.blogspot.com
There is the following Apple's working example, using sortedArrayUsingSelector:
and localizedCaseInsensitiveCompare:
in the Collections Documentation:
有以下 Apple 的工作示例,在Collections Documentation 中使用sortedArrayUsingSelector:
和:localizedCaseInsensitiveCompare:
sortedArray = [anArray sortedArrayUsingSelector:
@selector(localizedCaseInsensitiveCompare:)];
Note that this will return a new sorted array. If you want to sort your NSMutableArray
in place, then use sortUsingSelector:
instead, like so:
请注意,这将返回一个新的排序数组。如果您想NSMutableArray
就地排序,请sortUsingSelector:
改用,如下所示:
[mutableArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
回答by aksh1t
Here's an updated answer for Swift:
这是Swift的更新答案:
Sorting can be done in Swift with the help of closures. There are two methods -
sort
andsorted
which facilitate this.var unsortedArray = [ "H", "ello", "Wo", "rl", "d"] var sortedArray = unsortedArray.sorted {
.localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }unsortedArray.sort {
.localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }var unsortedArray = [ "H", "ello", "Wo", "rl", "d"] var sortedArray = unsortedArray.sorted {
.localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }unsortedArray.sort {
.localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }stringArray.sort{
.lowercaseString < .lowercaseString }stringArray.sort{
< }NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];
Note: Here
sorted
will return a sorted array. TheunsortedArray
itself will not be sorted.If you want to sort
unsortedArray
itself, use thisNSSortDescriptor * sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];
排序可以在 Swift 中借助闭包来完成。有两种方法-
sort
并sorted
有利于这一点。areas = [areas sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
注意:这里
sorted
将返回一个已排序的数组。该unsortedArray
本身不会进行排序。如果你想自己排序
##代码##unsortedArray
,使用这个
Reference :参考 :
Hereis the documentation for Swift's sorting methods.
Docs for different
String
comparison methods here.
Optional:
可选:
Instead of using localizedCaseInsensitiveCompare
, this could also be done:
除了使用localizedCaseInsensitiveCompare
,还可以这样做:
or even
甚至
##代码##if you want a case sensitive comparison
如果您想要区分大小写的比较
回答by Kupendiran iOS
It's easy to get the ascending order. Follow the bellow steps,,,
很容易得到升序。按照以下步骤,,,
Model 1 :
模型 1:
##代码##If you want the sorting to be case insensitive, you would need to set the descriptor like this,,
Model 2 :
如果您希望排序不区分大小写,则需要像这样设置描述符,
模型 2:
回答by Narasimha Nallamsetty
For me this worked....
对我来说这有效....
##代码##Here areas is NSArray
. I hope it helps someone.
这里的区域是NSArray
。我希望它可以帮助某人。