在objective-c中对NSMutableArray进行排序?

时间:2011-10-28 07:53:28

标签: xcode sorting nsmutablearray

我正在处理包含“First Name”列表的NSMutable Array。我想按字母顺序对它进行排序。

我试过这段代码

NSSortDescriptor * sortFriend = [[[NSSortDescriptor alloc] initWithKey:kfirstName ascending:YES] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortFriend];
NSArray * sorted = [Friendsarr sortedArrayUsingDescriptors:descriptors];

如果我错了,请纠正我。如果可以使用,我还有一系列名称。

由于


我尝试了代码

NSSortDescriptor * sortFriend = [[[NSSortDescriptor alloc] initWithKey:kfirstName ascending:YES  selector: @selector(caseInsensitiveCompare:)] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortFriend];
NSArray * arrmp = [temparray sortedArrayUsingDescriptors:descriptors];

但我的结果仍然包含2'k'字符。一个'k'和'K'。我希望得到不区分大小写的结果

3 个答案:

答案 0 :(得分:1)

如果您的NSMutableArray只包含NSString类型的对象,只需执行:

[myMutableArray sortUsingSelector:@selector(compare:)];

答案 1 :(得分:1)

您可以通过此代码

对NSMutablearray不敏感地排序
 NSSortDescriptor *sorter=[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:@selector(caseInsensitiveCompare:)]autorelease];
                NSArray *sortdescriptor=[NSArray arrayWithObject:sorter];
                [cpy_arr_Service_Name sortUsingDescriptors:sortdescriptor];

答案 2 :(得分:0)

我找到了使用示例代码对NSMutableArray进行排序的解决方案,该代码可以在下面的网站上找到。

http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/

我希望它可以帮助其他人,因为它使用数组作为数据源提供分组表视图的索引排序