Skip to content

Instantly share code, notes, and snippets.

@JaviSoto
Created June 4, 2013 03:46
Show Gist options
  • Save JaviSoto/5703432 to your computer and use it in GitHub Desktop.
Save JaviSoto/5703432 to your computer and use it in GitHub Desktop.
Natural String Sorting
@implementation NSString (JSRemoveIrrelevantTags)
- (NSString *)js_stringByRemovingIrrelevantTags
{
NSMutableString *filteredString = [NSMutableString string];
NSSet *filteredLinguisticTags = [NSSet setWithArray:@[NSLinguisticTagDeterminer, NSLinguisticTagPreposition]];
[self enumerateLinguisticTagsInRange:NSMakeRange(0, self.length)
scheme:NSLinguisticTagSchemeLexicalClass
options:NSLinguisticTaggerJoinNames | NSLinguisticTaggerOmitOther | NSLinguisticTaggerOmitWhitespace
orthography:nil
usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
if (![filteredLinguisticTags containsObject:tag])
{
[filteredString appendFormat:@"%@ ", [self substringWithRange:tokenRange]];
}
}];
return filteredString;
}
@end
@implementation NSArray (JSNatualSort)
- (NSArray *)js_naturallySortedArray
{
return [self sortedArrayUsingComparator:^NSComparisonResult(NSString *string1, NSString *string2) {
return [[string1 js_stringByRemovingIrrelevantTags] caseInsensitiveCompare:[string2 js_stringByRemovingIrrelevantTags]];
}];
}
@end
@JaviSoto
Copy link
Author

JaviSoto commented Jun 4, 2013

Line 16:

[filteredString appendFormat:@"%@ ", [self substringWithRange:tokenRange]];

is a hack, but it worked for the test :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment