All,
I would like to sort this mutable dictionary of arrays keys like so: A-Z 0-9 but it’s coming back sorted 0-9 A-Z. How can I sort it such that the alpha chars come before the numbers? Perhaps there is a built-in method that does this or should I create a category that extends NSString?
NSArray *sKeysArray = [[listContent allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *sectionKey = [sKeysArray objectAtIndex:section];
NSArray *sectionValues = [listContent valueForKey:sectionKey];
Sadly there’s no method in
NSStringthat will directly do this. Instead of writing a category, I would just useNSArrayssortedArrayUsingComparator:method:To do each comparison, I would use
NSString‘s– enumerateSubstringsInRange:options:usingBlock:passingNSStringEnumerationByComposedCharacterSequencesfor options (basically enumerating characters, except that unicode characters sequences which are actually one “letter” are combined). And when you’re comparing two characters, use something like the answer to this question to detect when obj1 is a number and obj2 is regular letter and returnNSOrderedDescending, otherwise just use regularcompare:.