Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
排序忽略标点符号(Objective-C)_Objective C_Sorting - Fatal编程技术网

排序忽略标点符号(Objective-C)

排序忽略标点符号(Objective-C),objective-c,sorting,Objective C,Sorting,我正在尝试对iOS UITableView对象进行排序。我目前正在使用以下代码: // Sort terms alphabetically, ignoring case [self.termsList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 这对我的名单进行了排序,惠斯特忽略了这个案子。不过,也可以忽略标点符号。例如: c.a.t. car cat 应分类如下: car c.a.t. cat (事实上,

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

// Sort terms alphabetically, ignoring case
[self.termsList sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
这对我的名单进行了排序,惠斯特忽略了这个案子。不过,也可以忽略标点符号。例如:

c.a.t.
car
cat
应分类如下:

car
c.a.t.
cat
(事实上,两只猫(猫或c.a.t.)中哪一只先到并不重要,只要它们排在另一只的旁边)

有没有一个简单的方法来解决这个问题?我认为解决方案将涉及从字符串中提取字母数字字符,然后比较这些字符,然后将它们返回到以前的状态,并再次包含非字母数字字符

事实上,我真正关心的字符只有句点(.),但如果有一个解决方案可以轻松涵盖所有标点符号,那么了解它会很有用

注:我一个月前问过这个问题。现在,我正在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:)];

这提供了排序的“ArrayWithOutpunchuation”,但当然不包含标点符号。这是不好的,因为尽管它现在被很好地排序,但它不再包含标点符号,而标点符号对于数组来说首先是至关重要的。我需要做的是按照“ArrayWithOutpunchuation”的顺序对“ArrayWith标点符号”排序。。。非常感谢您的帮助。

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

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];
    }];
这可能不是最有效的代码实际上,另一种选择是首先在数组上迭代并删除所有不需要的字符,然后使用
选择器
caseInsensitiveCompare
方法:

 NSString* yourStringSorted = [yourStringList sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

我的解决方案是将每个字符串分组为一个具有两个属性的自定义对象

  • 原始字符串
  • 没有标点符号的字符串
…然后根据不带标点符号的字符串对对象进行排序

目标C有一些简便的方法。 假设这个对象中有两个字符串:

NSString *myString;
NSString *modified;
首先,将自定义对象添加到数组中

NSMutableArray *myStrings = [[NSMutableArray alloc] init];
[myStrings addObject: ...];
然后,使用方便的NSSortDescriptor按修改后的变量对数组进行排序

//You can specify the variable name to sort by
//Sorting is done according to the locale using localizedStandardCompare
NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"modified" ascending:YES selector:@selector(localizedStandardCompare:)];
[myStrings sortedArrayUsingDescriptors:@[ mySortDescriptor ]];

瞧!对象(和字符串)已排序

这更干净,效率更高:

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

为了提高效率,我会编写一个忽略标点符号的比较方法,这样比较就不需要内存分配。

只需使用自定义比较器。到目前为止,我还无法实现它。一旦我能让它工作起来,我就会接受。对不起,我想我把你弄糊涂了。你只需要我的第一个建议——你试过了吗?我已经为此挣扎了好几天了。我正在尝试实施你的第二个建议(我无法实施第一个);请查看我对我的帖子的编辑@队长:蒂盖罗的第一个建议会满足你的要求。正如@imose在上面所做的,这对我的设置不起作用。我不知道为什么,但带句号的字符串仍然高于不带句号的字符串,无论我使用哪种解决方案。。。这肯定是由这段代码引起的,因为这只发生在我尝试排序算法(任何类型)之后。因此,排序(某种)正在发生。我会尝试深入挖掘,但在我发现为什么这仍然不起作用之前,赏金可能会到期;此代码仍然会导致拆分为单独的单词。例如,数组[cat,c.a.t和car]是按[c.a.t,car,cat]的顺序组织的。与Tiguero的第一个代码一样,该代码仍然使用连字符和句号对我的术语进行排序,并将其置于不带。。。我会想办法,但看起来赏金会先过期我已经检查过了,它对我来说很好,使用这个数组,@[..c“,@“a”,“a”,“b”,“b…”,@“a…”;它给出了,(a,“a”,“a…”,b,“b…”,“…c”)@CaptainProg,这个代码(和@tiguero的)工作得很好。要么你的问题有误导性,要么你漏掉了别的东西。与您的排序方法不同-它不会用空格替换标点符号,因此“c.a.t.”和“cat”是相等的,再次与您的代码不同,您的代码会赋予两者不同的权重。我已取消选择此答案,因为此代码仍会导致拆分为单独的单词。例如,数组[cat,c.a.t和car]按[c.a.t,car,cat]的顺序组织