Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort ignoring punctuation (Objective-C)

I am trying to sort an iOS UITableView object. I am currently using the following code:

// Sort terms alphabetically, ignoring case
[self.termsList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

This sorts my list, whist ignoring case. However, it would be nice to ignore punctuation as well. For example:

c.a.t.
car
cat

should be sorted as follows:

car
c.a.t.
cat

(It doesn't actually matter which of the two cats (cat or c.a.t.) comes first, so long as they're sorted next to one another).

Is there a simple method to get around this? I presume the solution would involve extracting JUST the alphanumeric characters from the strings, then comparing those, then returning them back to their former states with the non-alphanumeric characters included again.

In point of fact, the only characters I truly care about are periods (.) but if there is a solution that covers all punctuation easily then it'd be useful to know.

Note: I asked this exact same question of Java a month ago. Now, I am creating the same solution in Objective-C. I wonder if there are any tricks available for the iOS API that make this easy...

Edit: I have tried using the following code to strip punctuation and populate another array which I sort (suggested by @tiguero). However, I don't know how to do the last step: to actually sort the first array according to the order of the second. Here is my code:

    NSMutableArray *arrayWithoutPunctuation = [[NSMutableArray alloc] init];
    for (NSString *item in arrayWithPunctuation)
    {
        // Replace hyphens/periods with spaces
        item = [item stringByReplacingOccurrencesOfString:@"-" withString:@" "]; // ...hyphens
        item = [item stringByReplacingOccurrencesOfString:@"." withString:@" "]; // ...periods
        [arrayWithoutPunctuation addObject:item];
    }
    [arrayWithoutPunctuation sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

This provides 'arrayWithoutPunctuation' which is sorted, but of course doesn't contain the punctuation. This is no good, since, although it is now sorted nicely, it no longer contains punctuation which is crucial to the array in the first place. What I need to do is sort 'arrayWithPunctuation' according to the order of 'arrayWithoutPunctuation'... Any help appreciated.

like image 760
CaptainProg Avatar asked Oct 24 '12 21:10

CaptainProg


2 Answers

You can use a comparison block on an NSArray and your code will look like the following:

NSArray* yourStringList = [NSArray arrayWithObjects:@"c.a.t.", @"car", @"cat", nil];
NSArray* yourStringSorted = [yourStringList sortedArrayUsingComparator:^(id a, id b){
        NSString* as = (NSString*)a;
        NSString* bs = (NSString*)b;

        NSCharacterSet *unwantedChars = [NSCharacterSet characterSetWithCharactersInString:@"\\.:',"];
        //Remove unwanted chars
        as = [[as componentsSeparatedByCharactersInSet: unwantedChars] componentsJoinedByString: @""];
        bs = [[as componentsSeparatedByCharactersInSet: unwantedChars] componentsJoinedByString: @""];

        // make the case insensitive comparison btw your two strings
        return [as caseInsensitiveCompare: bs];
    }];

This might not be the most efficient code actually one other option would be to iterate on your array first and remove all unwanted chars and use a selector with the caseInsensitiveCompare method:

 NSString* yourStringSorted = [yourStringList sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
like image 179
tiguero Avatar answered Oct 17 '22 12:10

tiguero


This is a bit cleaner, and a bit more efficient:

NSArray* strings = @[@".....c",@"a.",@"a",@"b",@"b...",@"a..,"];
NSArray* sorted_strings = [strings sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString* a = [obj1 stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
    NSString* b = [obj2 stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
    return [a caseInsensitiveCompare:b];
}];

For real efficiency, I'd write a compare method that ignores punctuation, so that no memory allocations would be needed just to compare.

like image 23
Moshe Gottlieb Avatar answered Oct 17 '22 12:10

Moshe Gottlieb