I have a NSArray
of NSString
s.
For example: @"Good item", @"Very good item", @"The best of the best item"
Upon searching on UITextField
, the user types @"Very good item"
. I need to sort the source array by relevance and would like to see result as:
@"Very good item" <- the best match with the search phrase
@"Good item"
@"The best of the best item"
Please suggest me the best way to do it in Objective-C.
Thanks.
You write a method (maybe in an NSString category) that compares two strings and returns a number describing their similarity, using an algorithm that you need to design. A simple algorithm would be splitting each string into words, and counting the number of words the strings have in common.
Sorting is then trivial.