问题描述
有没有一种简单的方法来排序包含NSString的NSMutableArrays的一个NSMutableArray?
Is there an easy way to sort an NSMutableArray of NSMutableArrays containing NSStrings?
我相信,必须有这样一个简单的方法,但我似乎无法找到它。
I am sure that there must be an easy method for doing this but I can't seem to find it.
要澄清我想第一个按字母顺序排列的的NSString在子数组的索引3排序。
To Clarify I want to sort the first array alphabetically by the NSString at index 3 of the sub array.
推荐答案
没有看到任何人实际上比其他回答这个使用此功能,看着办吧。因此,这里是一些实际的code,你可以使用。
Didn't see anyone actually answering this with other than "use this function, figure it out". So here is some actual code that you can use.
将这个类别在您的.h文件的末尾(或任何地方,并导入在任何你需要排序的.H)
Put this category at the end of your .h file (or anywhere and import the .h where ever you need to sort)
@interface NSString (SortCompare)
-(NSInteger)stringCompare:(NSString *)str2;
@end
在.m文件(或要导入的.H的.M)
将这个
Put this in the .m file (or the .m of the .h you're importing)
@implementation NSString (SortCompare)
-(NSInteger) stringCompare:(NSString *)str2
{
return [(NSString *)self localizedCaseInsensitiveCompare:str2];
}
@end
现在通过调用sortUsingSelector使用的排序:在阵列结果
NSMutableArray的:
Now use the sort by calling sortUsingSelector: on the array
NSMutableArray:
[myArray sortUsingSelector:@selector(stringCompare:)];
NSArray的:
NSArray:
[myArray sortedArrayUsingSelector:@selector(stringCompare:)];
这篇关于一个人怎么可以排序含NSString的NSMutableArrays的一个NSMutableArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!