NSString 的 NSArray。按相关性排序。最佳实践

NSArray of NSStrings. Sort by relevance. Best practice

我有 NSArrayNSString

例如:@"Good item", @"Very good item", @"The best of the best item"

UITextField 上搜索时,用户键入 @"Very good item"。我需要按相关性对源数组进行排序,并希望看到结果为:

@"Very good item" <- the best match with the search phrase
@"Good item"
@"The best of the best item"

请在 Objective-C 中建议我最好的方法。

谢谢。

您编写一个方法(可能在 NSString 类别中)比较两个字符串和 returns 一个描述它们相似性的数字,使用您需要设计的算法。一个简单的算法是将每个字符串拆分成单词,然后计算字符串共有的单词数。

然后排序就变得微不足道了。