IOS/Objective-C:按字符串中的字数对NSString的NSArray排序
我有一个字符串数组,我想按每个字符串中的字数排序。然而,我在字典和数组方面很弱,并且很难有效地做到这一点 一种方法可能是将每个字符串放在一个包含字符串和单词数的字典中,然后使用NSSortDescriptor按单词数对字典进行排序 比如:IOS/Objective-C:按字符串中的字数对NSString的NSArray排序,ios,objective-c,nsarray,Ios,Objective C,Nsarray,我有一个字符串数组,我想按每个字符串中的字数排序。然而,我在字典和数组方面很弱,并且很难有效地做到这一点 一种方法可能是将每个字符串放在一个包含字符串和单词数的字典中,然后使用NSSortDescriptor按单词数对字典进行排序 比如: NSArray *myWordGroups = @[@"three",@"one two three",@"one two"]; NSMutableArray *arrayOfDicts=[NSMutableArray new]; for (i=0;i<
NSArray *myWordGroups = @[@"three",@"one two three",@"one two"];
NSMutableArray *arrayOfDicts=[NSMutableArray new];
for (i=0;i<[myWordGroups count];i++) {
long numWords = [myWordGroups[i] count];
//insert word and number into dictionary and add dictionary to new array
}
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"numWords"
ascending:NO];
NSArray *sortedArray = [myWords sortedArrayUsingDescriptors:@[sortDescriptor]];
NSArray*myWordGroups=@[@“三”、“一二三”、“一二三”];
NSMUTABLEARRY*arrayOfDicts=[NSMUTABLEARRY new];
对于(i=0;i
我有一个字符串数组,我想按每个字符串中的字数排序
首先编写一个实用方法,该方法接受一个字符串并返回其中的字数(无论对您来说意味着什么)。然后根据对每个元素调用实用方法的结果调用对字符串数组进行排序。一个简单的实现(假设语句中的单词由空格分隔)可能如下所示:
// create a comparator
NSComparisonResult (^comparator)(NSString *, NSString *) = ^ (NSString *firstString, NSString *secondString){
NSUInteger numberOfWordsInFirstString = [firstString componentsSeparatedByString:@" "].count;
NSUInteger numberOfWordsInSecondString = [secondString componentsSeparatedByString:@" "].count;
if (numberOfWordsInFirstString > numberOfWordsInSecondString) {
return NSOrderedDescending;
} else if (numberOfWordsInFirstString < numberOfWordsInSecondString) {
return NSOrderedAscending;
} else {
return NSOrderedSame;
}
};
NSArray *strings = @[@"a word", @"even more words", @"a lot of words", @"more words", @"i can't even count the words"];
// use the comparator to sort your array of strings
NSArray *stringsSortedByNumberOfWords = [strings sortedArrayUsingComparator:comparator];
NSLog(@"%@", stringsSortedByNumberOfWords);
// results in:
// "a word",
// "more words",
// "even more words",
// "a lot of words",
// "i can't even count the words"
//创建一个比较器
NSComparisonResult(^comparator)(NSString*,NSString*)=^(NSString*第一个字符串,NSString*第二个字符串){
NSUniter numberOfWordsInFirstString=[firstString组件由字符串分隔:@”“]。计数;
NSU Integer NumberOfWordsSecondString=[secondString组件由字符串分隔:@”“]。计数;
如果(numberOfWordsInFirstString>numberOfWordsInSecondString){
退而求其次;
}else if(numberOfWordsInFirstString
这是Objective-C代码
NSArray *myWordGroups = @[@"three",@"one two three",@"one two"];
NSArray *sortedStrings = [myWordGroups sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self.length" ascending:YES]]];
// => Sorted Array : @[@"three", @"one two", @"one two three"]
您应该首先使用字数计算方法展开NSString
:
@interface NSString(WordCount)
- (NSUInteger)wordCount;
@end
@implementation NSString(WordCount)
- (NSUInteger)wordCount
{
// There are several ways to do this. Pick up your own on SO or another place of the internet. I took this one:
__block NSUInteger count = 0;
[self enumerateSubstringsInRange:NSMakeRange(0, string.length)
options:NSStringEnumerationByWords
usingBlock:
^(NSString *character, NSRange substringRange, NSRange enclosingRange, BOOL *stop)
{
count++;
}];
return count;
}
这有以下优点:
- 您可以出于其他原因使用此方法
- 字数是字符串的一个属性,因此该方法应该是类
NSString
的成员
现在,您可以简单地使用排序描述符:
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"wordCount" ascending:YES];
NSArray *sortedStrings = [myWordGroups sortedArrayUsingDescriptors:@[sorter]];
您可以按此处所述获取字数:您将代码放在comparator块中对字数进行计数并进行比较?所需排序取决于字数,而不是字符串长度。代码进行编译,但尝试访问字数(或字数)正在抛出一个无法识别的选择器,除非它与Koen提供的链接中的一个单词计数方法一起工作。问题是因为wordCount是NSString上的一个方法,它需要为字符串引用self,而不是作为参数引用string。在Amin的方法中,我想您也应该更改为self。哦,是的,…我更改了它。