sorting NSArray of NSStrings by match occurrence
问题 How can I sort NSArray by search pattern matching? So if for example I have a search pattern equal 'xd' and an array of values: axd bxd xdd gtxd xdc how can I get the output like below: xdc xdd axd bxd gtxd Thank you in advance. 回答1: Use NSArray 's sortedArrayUsingFunction: with a function that orders first by the position of the search term, then by the strings' natural ordering. NSInteger sorter(id arg1, id arg2, void *context) { NSString *searchTerm = (NSString *)context; NSRange range1 =