我的意思是一个数组,其中包含以下单词:
@[@"Hellobla", @"Hello Kitty", @"Hello Kitty Bla", @"Bla apukitty", @"Bla helkitty"]
我需要对它们进行排序,就像锚定由空格分隔的所有单词一样@" "
。
如果我输入"Hel"
- 我会看到@"Hellobla", @"Hello Kitty Bla", @"Bla helkitty"
如果我输入"kit"
- 我会看到@"Hello Kitty Bla", @"Hello Kitty"
如果我输入"bla"
- 我会看到@"Hello Kitty Bla", @"Bla apukitty", @"Bla helkitty"
ETC :)
我在块返回数组中执行此操作:
// arrayWithData - All Words
// searchWord - Text i try to search with
// arrayToWrite - array to return with words
// arrayToCompare - array with words already added whitch first NSPredicate *beginPredicate = [NSPredicate predicateWithFormat:@"self BEGINSWITH[cd] %@", //searchText];
beginMatch = [self.objects filteredArrayUsingPredicate:beginPredicate];
sortedWithSecondWordArray = ^(NSArray * arrayWithData,
NSString * searchWord,
NSMutableArray *arrayToWrite,
NSArray * arrayToCompare) {
NSString *space = @" ";
for (__strong NSString *string in arrayWithData) {
NSString *staticString = string;
while ([string rangeOfString:space].location != NSNotFound) {
NSRange range = NSMakeRange(0, [string rangeOfString:space].location);
NSString *keyword = [[string stringByReplacingCharactersInRange:range withString:@""] trim];
if (keyword.length > 0
&& [keyword rangeOfString:searchWord options:NSAnchoredSearch].location != NSNotFound
&& ![arrayToCompare containsObject:staticString]
&& ![arrayToWrite containsObject:staticString]) {
[arrayToWrite addObject:staticString];
}
string = keyword;
};
}
return arrayToWrite;
};
它有效,但如果在我的 Array 2000 个字符串中,它可能超过 10k。它真的很糟糕。如何优化它?