1

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.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Igor Jorin
  • 141
  • 10

1 Answers1

0

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.

gnasher729
  • 51,477
  • 5
  • 75
  • 98