如何将NSMutableArray中的字符串按字母顺序排序?

时间:2021-07-02 16:00:21

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?

我怎么做呢?

4 个解决方案

#1


128  

There is the following Apple's working example, using sortedArrayUsingSelector: and localizedCaseInsensitiveCompare: in the 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:)];

#2


8  

Here's an updated answer for Swift:

以下是Swift的更新答案:

  • Sorting can be done in Swift with the help of closures. There are two methods - sort and sorted which facilitate this.

    在闭包的帮助下,可以快速进行排序。有两种方法——排序和排序。

    var unsortedArray = [ "H", "ello", "Wo", "rl", "d"]
    var sortedArray = unsortedArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
    

    Note: Here sorted will return a sorted array. The unsortedArray itself will not be sorted.

    注意:这里排序将返回一个排序后的数组。unsortedArray本身不会被排序。

  • If you want to sort unsortedArray itself, use this

    如果你想排序unsortedArray本身,就用这个。

    unsortedArray.sort { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
    


Reference :

  • Here is the documentation for Swift's sorting methods.

    以下是Swift排序方法的文档。

  • Docs for different String comparison methods here.

    用于不同字符串比较方法的文档。


Optional:

可选:

Instead of using localizedCaseInsensitiveCompare, this could also be done:

也可以这样做,而不是使用本地化的caseinsensitivecompare:

stringArray.sort{ $0.lowercaseString < $1.lowercaseString }

or even

甚至

stringArray.sort{ $0 < $1 }

if you want a case sensitive comparison

如果你想要区分大小写

#3


1  

It's easy to get the ascending order. Follow the bellow steps,,,

很容易得到升序。遵循波形的步骤,,,

Model 1 :

模型1:

NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];

If you want the sorting to be case insensitive, you would need to set the descriptor like this,,
Model 2 :

如果你想让排序不区分大小写,你需要像这样设置描述符,模型2:

NSSortDescriptor * sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];

#4


0  

For me this worked....

对我而言,这工作....

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

Here areas is NSArray. I hope it helps someone.

这里是NSArray领域。我希望它能帮助某人。

#1


128  

There is the following Apple's working example, using sortedArrayUsingSelector: and localizedCaseInsensitiveCompare: in the 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:)];

#2


8  

Here's an updated answer for Swift:

以下是Swift的更新答案:

  • Sorting can be done in Swift with the help of closures. There are two methods - sort and sorted which facilitate this.

    在闭包的帮助下,可以快速进行排序。有两种方法——排序和排序。

    var unsortedArray = [ "H", "ello", "Wo", "rl", "d"]
    var sortedArray = unsortedArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
    

    Note: Here sorted will return a sorted array. The unsortedArray itself will not be sorted.

    注意:这里排序将返回一个排序后的数组。unsortedArray本身不会被排序。

  • If you want to sort unsortedArray itself, use this

    如果你想排序unsortedArray本身,就用这个。

    unsortedArray.sort { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending }
    


Reference :

  • Here is the documentation for Swift's sorting methods.

    以下是Swift排序方法的文档。

  • Docs for different String comparison methods here.

    用于不同字符串比较方法的文档。


Optional:

可选:

Instead of using localizedCaseInsensitiveCompare, this could also be done:

也可以这样做,而不是使用本地化的caseinsensitivecompare:

stringArray.sort{ $0.lowercaseString < $1.lowercaseString }

or even

甚至

stringArray.sort{ $0 < $1 }

if you want a case sensitive comparison

如果你想要区分大小写

#3


1  

It's easy to get the ascending order. Follow the bellow steps,,,

很容易得到升序。遵循波形的步骤,,,

Model 1 :

模型1:

NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];

If you want the sorting to be case insensitive, you would need to set the descriptor like this,,
Model 2 :

如果你想让排序不区分大小写,你需要像这样设置描述符,模型2:

NSSortDescriptor * sortDesc = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 
sortedArray=[yourArray sortedArrayUsingDescriptors:@[sortDesc]];

#4


0  

For me this worked....

对我而言,这工作....

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

Here areas is NSArray. I hope it helps someone.

这里是NSArray领域。我希望它能帮助某人。