Search code examples
objective-csortingalphabetgrapheme

How do I identify which letter of the alphabet a word starts with in Objective-C?


Given a string, I'm trying to determine which letter of the alphabet it belongs to. For example, "apple" goes into the "A" section. "Banana" goes into the "B" section. I'm using this to identify the section:

NSRange range = [string rangeOfString:letter 
                              options:NSAnchoredSearch |
                                      NSCaseInsensitiveSearch |
                                      NSDiacriticInsensitiveSearch |
                                      NSWidthInsensitiveSearch
                                range:NSMakeRange(0, string.length)
                               locale:locale];

Where string is the string I'm trying to bucket and letter is a letter of the alphabet. I do this in a loop for each letter of the alphabet.

It works great, except for words like "æquo", which should be bucketed into the letter "A", but aren't. What to do?

Edit The plot thickens. I'm looking at Korean now. The word "것" should be bucketed into the letter "ㄱ". There's got to be some way to do this other than maintaining a huge mapping table.


Solution

  • I think I've figured it out: I was thinking about it wrong. The question isn't, does a given word begin with a certain letter of the alphabet. Rather, the question is, does a given word fall within the sorting range of a certain letter of the alphabet.

    For example, in the case of "æquo", I can check if it falls within the sorting range of the "A" section by checking if it is or comes after "A", and comes before "B".

    Apple's compare:options:range:locale: method knows the answer to those two questions for any given locale. In this particular example, for French it would say yes. For some other language, like Danish, it should say no.

    I've tested this on English, Spanish, Portuguese, French, German, and Korean, and it appears to be giving the expected results.