Saturday, May 18, 2013

NSString localizedCaseInsensitiveCompare example ios


[NSString localizedCaseInsensitiveCompare]

Returns an NSComparisonResult value that indicates the lexical ordering of the receiver and a given string using a case-insensitive, localized, comparison.
- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)aString
Parameters
aString
The string with which to compare the receiver.
This value must not be nil. If this value is nil, the behavior is undefined and may change in future versions of OS X.
Return Value of [NSString localizedCaseInsensitiveCompare]
NSOrderedAscending the receiver precedes aString in lexical ordering,NSOrderedSame the receiver and aString are equivalent in lexical value, andNSOrderedDescending if the receiver follows aString.
Example of [NSString localizedCaseInsensitiveCompare]
NSArray *sorted = [foo sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
    // Remove all spaces
    NSString *s1 = [str1 stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *s2 = [str2 stringByReplacingOccurrencesOfString:@" " withString:@""];

    return [s1 localizedCaseInsensitiveCompare:s2];
}];
Example of [NSString localizedCaseInsensitiveCompare]
NSArray *sortedValues = [[yourDictionary allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableDictionary *orderedDictionary=[[NSMutableDictionary alloc]init];
for(NSString *valor in sortedValues){
    for(NSString *clave in [yourDictionary allKeys]){
        if ([valor isEqualToString:[yourDictionary valueForKey:clave]]) {
            [orderedDictionary setValue:valor forKey:clave];
        }
    }
}
Example of [NSString localizedCaseInsensitiveCompare]
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];