Saturday, May 18, 2013

NSString localizedCompare example ios


[NSString localizedCompare]

Returns an NSComparisonResult value that indicates the lexical ordering of the receiver and another given string using a localized comparison.
- (NSComparisonResult)localizedCompare:(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 localizedCompare]
NSOrderedAscending the receiver precedes string in lexical ordering,NSOrderedSame the receiver and string are equivalent in lexical value, andNSOrderedDescending if the receiver follows string.

Example of [NSString localizedCompare]
NSArray *sorted = [foo sortedArrayUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2) {
    // Remove all spaces
    NSString *s1 = [str1 stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *s2 = [str2 stringByReplacingOccurrencesOfString:@" " withString:@""];

    return [s1 localizedCompare:s2];
}];
Example of [NSString localizedCompare]
NSArray *sortedValues = [[yourDictionary allValues] sortedArrayUsingSelector:@selector(localizedCompare:)];
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 localizedCompare]
sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCompare:)];