IOS/Objective-C: 按字符串中的单词数对 NSStrings 的 NSArray 进行排序
IOS/Objective-C: Sort NSArray of NSStrings by number of words in string
我有一个字符串数组,我想按每个字符串中的单词数对其进行排序。然而,我在字典和数组方面很弱,并且无法有效地做到这一点。
一种方法可能是将每个字符串放在一个字典中,字典中包含字符串和其中的单词数,然后使用 NSSortDescriptor 按单词数对字典进行排序。
类似于:
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]];
我不清楚将单词和单词数量添加到词典中的代码。即使我知道该代码,这看起来也非常麻烦。
有没有一种方法可以根据每个字符串中的单词数快速对字符串数组进行排序?
提前感谢您的任何建议。
I have an array of strings that I would like to sort by the number of words in each string
首先编写一个实用程序方法,它接受一个字符串和 returns 其中的单词数(无论这对您意味着什么)。然后调用 sortedArrayUsingComparator:
根据对每个元素调用实用程序方法的结果对字符串数组进行排序。
一个简单的实现(假设句子中的单词由 space 分隔)可能如下所示:
// 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"
这是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;
}
这样做的好处是:
- 您可以出于其他原因使用此方法。
- 字数是一个属性的字符串,所以方法应该是class
NSString
. 的成员
现在您可以简单地使用排序描述符:
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"wordCount" ascending:YES];
NSArray *sortedStrings = [myWordGroups sortedArrayUsingDescriptors:@[sorter]];
我有一个字符串数组,我想按每个字符串中的单词数对其进行排序。然而,我在字典和数组方面很弱,并且无法有效地做到这一点。
一种方法可能是将每个字符串放在一个字典中,字典中包含字符串和其中的单词数,然后使用 NSSortDescriptor 按单词数对字典进行排序。
类似于:
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]];
我不清楚将单词和单词数量添加到词典中的代码。即使我知道该代码,这看起来也非常麻烦。
有没有一种方法可以根据每个字符串中的单词数快速对字符串数组进行排序?
提前感谢您的任何建议。
I have an array of strings that I would like to sort by the number of words in each string
首先编写一个实用程序方法,它接受一个字符串和 returns 其中的单词数(无论这对您意味着什么)。然后调用 sortedArrayUsingComparator:
根据对每个元素调用实用程序方法的结果对字符串数组进行排序。
一个简单的实现(假设句子中的单词由 space 分隔)可能如下所示:
// 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"
这是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;
}
这样做的好处是:
- 您可以出于其他原因使用此方法。
- 字数是一个属性的字符串,所以方法应该是class
NSString
. 的成员
现在您可以简单地使用排序描述符:
NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"wordCount" ascending:YES];
NSArray *sortedStrings = [myWordGroups sortedArrayUsingDescriptors:@[sorter]];