Q:

Sort an NSArray of NSString by ignoring spaces

I've an 
NSArray *foo = @[@"Chicago Hts, IL", @"Chicago Heights, IL", @"Chicago, IL"];

i want to get a sorted array like this: 
@[@"Chicago, IL", @"Chicago Heights, IL", @"Chicago Hts, IL"];

i used
[foo sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:),

which results in 
@[@"Chicago Heights, IL", @"Chicago Hts, IL", @"Chicago, IL"];

This array has about 100k+ entries so splitting the string based on empty space and sorting it and 'unsplitting' would result in a bad perforamnce.
I realize that Finder sorts files the same way as 'localizedCaseInsensitiveCompare' so i guess i'm out of luck.
I'm leaning towards splitting the array based on the first character 'a', join the string based on the empty space, sort it and then display it to the user in a tableView and if the user scrolls past that first set of results, get the next set of objects for 'b', join the words by ignoring empty space, sort, 'un-join the words' and show.

A:

Provide your own comparator:
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];
}];

