NSArray of NSStrings. Sort by relevance. Best practice

77 views Asked by At

I have a NSArray of NSStrings.

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.

1

There are 1 answers

0
gnasher729 On BEST ANSWER

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.