Created
June 4, 2013 03:46
-
-
Save JaviSoto/5703432 to your computer and use it in GitHub Desktop.
Natural String Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 16:
is a hack, but it worked for the test :P