我想排序iOS UITableView对象。我目前使用下面的代码:排序忽略标点符号(Objective-C)
// Sort terms alphabetically, ignoring case
[self.termsList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
这个排序名单,惠斯特忽略大小写。但是,忽略标点符号也会很好。例如:
car
c.a.t.
cat
(实际上它并不无论哪两只猫(猫或猫)的至上,只要他们进行排序:
c.a.t.
car
cat
应如下排序彼此相邻)。
有没有简单的方法来解决这个问题?我认为解决方案将涉及从字符串中提取字母数字字符,然后比较这些字符串,然后将它们返回到以前的状态,并再次包含非字母数字字符。
事实上,我真正关心的唯一字符是句点(。),但是如果有一种方法可以很容易地覆盖所有标点符号,那么知道它会很有用。
注:我在一个月前询问exact same question of Java。现在,我正在Objective-C中创建相同的解决方案。我不知道是否有针对iOS的API,使这个易得什么花样......
编辑:我一直在使用下面的代码剥离标点和填充另一个阵列我排序(由@tiguero建议)尝试。但是,我不知道如何完成最后一步:按照第二个顺序实际排序第一个数组。这里是我的代码:
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:)];
这提供了排序“arrayWithoutPunctuation”,当然不包含标点符号。这是不好的,因为尽管它现在被很好地排序,但它不再包含对于数组来说至关重要的标点符号。我需要做的是根据'arrayWithoutPunctuation'的顺序排序'arrayWithPunctuation'...任何帮助表示赞赏。
只需使用自定义比较器即可。 –
我的回答无效吗? – tiguero
至今我还没有能够实现它。我会接受一次,我可以得到它的工作 – CaptainProg