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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:17:43  来源:igfitidea点击:

How can I sort strings in NSMutableArray into alphabetical order?

objective-cioscocoa-touchsortingnsmutablearray

提问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 NSMutableArrayin 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 - sortand sortedwhich facilitate this.

    var unsortedArray = [ "H", "ello", "Wo", "rl", "d"]
    var sortedArray = unsortedArray.sorted { 
    unsortedArray.sort { 
    var unsortedArray = [ "H", "ello", "Wo", "rl", "d"]
    var sortedArray = unsortedArray.sorted { 
    unsortedArray.sort { 
    stringArray.sort{ 
    stringArray.sort{ 
    NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
    sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];
    
    < }
    .lowercaseString < .lowercaseString }
    .localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }
    .localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }
    .localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }
    .localizedCaseInsensitiveCompare() == NSComparisonResult.OrderedAscending }

    Note: Here sortedwill return a sorted array. The unsortedArrayitself will not be sorted.

  • If you want to sort unsortedArrayitself, use this

    NSSortDescriptor * sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
    sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];
    
  • 排序可以在 Swift 中借助闭包来完成。有两种方法-sortsorted有利于这一点。

    areas = [areas sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    

    注意:这里sorted将返回一个已排序的数组。该unsortedArray本身不会进行排序。

  • 如果你想自己排序unsortedArray,使用这个

    ##代码##



Reference :参考 :

  • Hereis the documentation for Swift's sorting methods.

  • Docs for different Stringcomparison methods here.

  • 是 Swift 排序方法的文档。

  • 此处为不同String比较方法的文档。



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。我希望它可以帮助某人。