2013-10-29 132 views
1

我是新来的objective-c,ios。 我想按照alfabetic顺序排序一个名为filteredList的NSMutableArray,它包含NSString类型的对象。排序包含NSString对象的NSMutableArray

所以如果我的可变数组包含:玛丽,比尔,约翰]我想有[比尔,玛丽,约翰]

我做了以下内容:

[filteredList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

但我不看到任何变化。我没有阅读和尝试其他解决方案比较:而不是localizedCaseInsensitiveCompare,但仍然没有。

+0

https://developer.apple.com/library/ mac/documentation/Cocoa/Conceptual/Collections/Articles/Arrays.html#// apple_ref/doc/uid/20000132-SW5 or https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html –

+0

您的代码将可变数组*正确地排序为[Bill,John,Mary ]。请显示一个完整的独立(非)工作示例。 –

回答

2

更新

NSArray *sortedArray =[unSortedArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];//unSortedArray is NSMutableArray 
unSortedArray = [[NSMutableArray alloc]initWithArray:sortedArray]; 
+0

我拥有的数组是一个NSMutableArray。请问您可以根据要求更改您的代码吗? –

+0

unSortedArray是NSMutableArray,您只需将排序的值存储在NSArray(sortedArray)中。 – TamilKing

+0

@justME:我更新我的代码 – TamilKing

0

我会做这样的事情:

NSArray *sortedArray = [filteredList sortedArrayUsingComparator:^(NSString *str1, NSString *str2) { 
    return (NSComparisonResult)[str1 compare:str2]; 
}]; 
filteredList = [sortedArray mutableCopy]; 
+0

我已经检查过如果sortedArray的元素处于corect位置。他们是,但过滤列表仍然没有在我的桌面视图弹出。 –

+0

不好怎么样?不正确的顺序? – Zalykr

+0

是的,它们没有以正确的顺序显示在我的tableView –

0

请参考下面的代码: -

filteredList = (NSMutableArray*)[filteredList sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
相关问题