xcode 在objective-c 中对NSMutableArray 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7926531/
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
Sort a NSMutableArray in objective-c?
提问by vikas ojha
I am working on NSMutable Array containing list of "First Name" in it.I want to sort it alphabatically.
我正在处理包含“名字”列表的 NSMutable Array。我想按字母顺序对其进行排序。
I tried this code
我试过这个代码
NSSortDescriptor * sortFriend = [[[NSSortDescriptor alloc] initWithKey:kfirstName ascending:YES] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortFriend];
NSArray * sorted = [Friendsarr sortedArrayUsingDescriptors:descriptors];
correct me if i am wrong.I do have an array of names also if that could be used.
如果我错了,请纠正我。如果可以使用,我也确实有一组名称。
Thanks
谢谢
I tried the code
我试过代码
NSSortDescriptor * sortFriend = [[[NSSortDescriptor alloc] initWithKey:kfirstName ascending:YES selector: @selector(caseInsensitiveCompare:)] autorelease];
NSArray * descriptors = [NSArray arrayWithObject:sortFriend];
NSArray * arrmp = [temparray sortedArrayUsingDescriptors:descriptors];
But still my result contain 2 'k' chars. one 'k' and 'K'.I want to have case insensitive result
但我的结果仍然包含 2 个 'k' 字符。一个'k'和'K'。我想要不区分大小写的结果
回答by Rahul Vyas
If your NSMutableArray only contains objects of type NSString, simply do:
如果您的 NSMutableArray 只包含 NSString 类型的对象,只需执行以下操作:
[myMutableArray sortUsingSelector:@selector(compare:)];
回答by alok srivastava
You can sort NSMutable array case insensitively by this code
您可以通过此代码不区分大小写对 NSMutable 数组进行排序
NSSortDescriptor *sorter=[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES selector:@selector(caseInsensitiveCompare:)]autorelease];
NSArray *sortdescriptor=[NSArray arrayWithObject:sorter];
[cpy_arr_Service_Name sortUsingDescriptors:sortdescriptor];
回答by vikas ojha
I found the solution of sorting NSMutableArray
s with sample code which can be found on the site below.
我找到了NSMutableArray
使用示例代码对s进行排序的解决方案,可以在下面的网站上找到。
I hope it help others to as it provides indexed sorting of a grouped table view using an array as the datasource
我希望它可以帮助其他人,因为它提供使用数组作为数据源的分组表视图的索引排序