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.
You can use a comparison block on an
NSArrayand your code will look like the following: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
selectorwith thecaseInsensitiveComparemethod: