排序忽略标点(Objective-C) [英] Sort ignoring punctuation (Objective-C)

查看:105
本文介绍了排序忽略标点(Objective-C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对iOS UITableView对象进行排序.我目前正在使用以下代码:

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

应按以下顺序排序:

car
c.a.t.
cat

(两只猫(哪只猫是猫还是c.a.t.)先出现,实际上并不重要,只要它们紧挨着排序即可.)

(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.

注意:我一个月前问了这个与Java完全相同的问题.现在,我在Objective-C中创建相同的解决方案.我想知道是否有可用于iOS API的技巧可以使此操作变得简单...

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...

编辑:我尝试使用以下代码删除标点符号并填充另一个要排序的数组(由@tiguero建议).但是,我不知道如何做最后一步:实际上是根据第二个数组的顺序对第一个数组进行排序.这是我的代码:

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:)];

这提供了已排序的"arrayWithoutPunctuation",但当然不包含标点符号.这不好,因为尽管现在排序很好,但它不再包含标点符号,而标点符号首先对数组至关重要.我需要做的是按照"arrayWithoutPunctuation"的顺序对"arrayWithPunctuation"进行排序...任何帮助.

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.

推荐答案

您可以在NSArray上使用比较块,您的代码将如下所示:

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];
    }];

这实际上可能不是最有效的代码,另一个选择是首先在您的数组上进行迭代,并删除所有不需要的字符,然后将selectorcaseInsensitiveCompare方法一起使用:

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:)];

这篇关于排序忽略标点(Objective-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆