Monday, May 20, 2013

NSString NSForcedOrderingSearch example ios


Search and Comparison Options - NSForcedOrderingSearch

These values represent the options available to many of the string classes’ search and comparison methods.
enum {
   NSCaseInsensitiveSearch = 1,
   NSLiteralSearch = 2,
   NSBackwardsSearch = 4,
   NSAnchoredSearch = 8,
   NSNumericSearch = 64,
   NSDiacriticInsensitiveSearch = 128,
   NSWidthInsensitiveSearch = 256,
   NSForcedOrderingSearch = 512,
   NSRegularExpressionSearch = 1024
};
Constants
NSCaseInsensitiveSearch
A case-insensitive search.
NSLiteralSearch
Exact character-by-character equivalence.
NSBackwardsSearch
Search from end of source string.[NSString NSForcedOrderingSearch]
NSAnchoredSearch
Search is limited to start (or end, if NSBackwardsSearch) of source string.
NSNumericSearch
Numbers within strings are compared using numeric value, that is, Name2.txt <Name7.txt < Name25.txt.
This option only applies to compare methods, not find.
NSDiacriticInsensitiveSearch
Search ignores diacritic marks.[NSString NSForcedOrderingSearch]
For example, ‘รถ’ is equal to ‘o’.
NSWidthInsensitiveSearch
Search ignores width differences in characters that have full-width and half-width forms, as occurs in East Asian character sets.
For example, with this option, the full-width Latin small letter 'a' (Unicode code point U+FF41) is equal to the basic Latin small letter 'a' (Unicode code point U+0061).
NSForcedOrderingSearch
Comparisons are forced to return either NSOrderedAscending orNSOrderedDescending if the strings are equivalent but not strictly equal.
This option gives stability when sorting. For example, “aaa” is greater than "AAA” if NSCaseInsensitiveSearch is specified.
NSRegularExpressionSearch
The search string is treated as an ICU-compatible regular expression. If set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch. You can use this option only with the rangeOfString:...methods and stringByReplacingOccurrencesOfString:withString:options:range:.
Discussion of [NSString NSForcedOrderingSearch]
See “Searching, Comparing, and Sorting Strings” for details on the effects of these options.




Example of [NSString NSForcedOrderingSearch]

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {

@autoreleasepool {

    NSArray *stringsArray = [NSArray arrayWithObjects:
                             @"string 1",
                             @"String 21",
                             @"string 12",
                             @"String 11",
                             @"String 02", nil];
    static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch |
    NSWidthInsensitiveSearch | NSForcedOrderingSearch;
    NSLocale *currentLocale = [NSLocale currentLocale];
    NSComparator finderSort = ^(id string1, id string2) {
        NSRange string1Range = NSMakeRange(0, [string1 length]);
        return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];
    };

    NSArray* sortedArray = [stringsArray sortedArrayUsingComparator:finderSort];
    NSLog(@"finderSort: %@", sortedArray);        
    }
    return 0;
}

Example of [NSString NSForcedOrderingSearch]
- (void)sortSearchResultWithInDocumentTypeArray:(NSMutableArray *)aResultArray basedOn:(NSString *)aSearchString {

    NSSortDescriptor * frequencyDescriptor =[[NSSortDescriptor alloc] initWithKey:aSearchString ascending:YES comparator:^(id firstDocumentName, id secondDocumentName) {

        static NSStringCompareOptions comparisonOptions =
        NSCaseInsensitiveSearch | NSNumericSearch |
        NSWidthInsensitiveSearch | NSForcedOrderingSearch;

        return [firstDocumentName compare:secondDocumentName options:comparisonOptions];
     }];

    NSArray * descriptors =    [NSArray arrayWithObjects:frequencyDescriptor, nil];
    [aResultArray sortUsingDescriptors:descriptors];
}
Example of [NSString NSForcedOrderingSearch]
int finderSortWithLocale(id string1, id string2, void *locale)
{
    static NSStringCompareOptions comparisonOptions =
        NSCaseInsensitiveSearch | NSNumericSearch |
        NSWidthInsensitiveSearch | NSForcedOrderingSearch;

    NSRange string1Range = NSMakeRange(0, [string1 length]);

    return [string1 compare:string2
                    options:comparisonOptions
                    range:string1Range
                    locale:(NSLocale *)locale];
}