2

我得到一个包含以下字符串的字符串数组,这些字符串有一定的模式

str1 str51 str3 str4 str10 str39 str31 str191

每个字符串都以 'str' 开头,并在其末尾附加一个数字。

有没有一种巧妙的方法来对数组进行排序,以便按值顺序列出字符串

str1 str3 str4 str10 str31 str39 str51 str191

我可以通过编写一些递归函数来看到一种方法

NSString * firstString = [[strArray objectAtIndex:i].substringFromIndex:3];
NSString * secondString = [[strArray objectAtIndex:i+1].substringFromIndex:3];

if ([firstString intValue] < [secondString intValue])
{
    //do nothing they order is correct
}
else
{
    //swap the order of the 2 strings in the array
}

但那是非常基本的代码,Objective-C 中是否有一些机制或一个很好的代码技巧来更好地处理这种排序?

非常感谢,-代码

4

6 回答 6

6

如果它们都以相同的前缀开头,我相信默认排序会适当地处理它们。我的意思是:

[someArray sortUsingSelector:@selector(compare:)];

如果由于某种原因这不起作用,那么您可以使用块。尝试这个:

NSArray *sortedArray = [someArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) 
{
    //Default compare, to protect against cast
    if (![a isKindOfClass:[NSString class]] || ![b isKindOfClass:[NSString class]]) {
        return ([a compare:b]);
    }
    else {
        NSString *aString = (NSString*) a;
        NSString *bString = (NSString*) b;
        int aInt = [[a.substringFromIndex:3] intValue];
        int bInt = [[b.substringFromIndex:3] intValue];
        return [aInt < bInt];
    }
}];
于 2012-07-27T09:30:58.163 回答
2

你可以这样排序

NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSString * str1 = [(NSString *)obj1 substringFromIndex:3];
    NSString * str2 = [(NSString *)obj2 substringFromIndex:3];
    return [str1 compare:str2 options:NSNumericSearch];
}];
于 2012-07-27T09:35:41.150 回答
0

创建一个 NSSortDescriptor 并根据键对数组进行排序:“description”。像这样:

NSMutableArray *array = [NSMutableArray arrayWithCapacity:0]; //Your Array of NSStrings
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES];
array = [array sortedArrayUsingDescriptors:@[descriptor]];
于 2014-03-07T11:42:04.520 回答
0

您可以定义具有任何您想要的行为的比较器函数。下面的一个将模拟 Mac OS 中 Finder 的行为,忽略大小写并尊重字符串末尾出现的数字。但是,顺序将取决于提供的语言环境,因此如果您希望结果绝对一致,您应该选择一个语言环境并对其进行硬编码。

int finderSortWithLocale(id string1, id string2, void *locale)
{
    static NSStringCompareOptions comparisonOptions =
        NSCaseInsensitiveSearch | NSNumericSearch |
        NSWidthInsensitiveSearch | NSForcedOrderingSearch;

    NSRange string1Range = NSMakeRange(0, [string1 length]);

    return [string1 compare:string2
                    options:comparisonOptions
                    range:string1Range
                    locale:(__bridge NSLocale *)locale];
}

您绝对应该阅读有关每个选项的文档,以确保它符合您的要求。

要使用这样的比较器有效地对字符串数组进行排序,请使用以下sortedArrayUsingFunction:context:方法:

NSArray *sortedArray = [stringsArray sortedArrayUsingFunction:finderSortWithLocale context:(__bridge void *)[NSLocale currentLocale]];
于 2012-07-27T09:29:36.963 回答
0
NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"datetime"
                                                             ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortByName];
NSArray *sortedArray = [myArrayNonsort sortedArrayUsingDescriptors:sortDescriptors];
于 2014-09-01T06:37:08.513 回答
-1

sortedArray = [anArray sortedArrayUsingSelector: @selector(localizedCaseInsensitiveCompare:)];

输入:anArray 输出:sortedArray

参考文档

于 2012-07-27T09:31:48.397 回答