如何在objective-C中对字符串进行排序?

时间:2021-04-10 19:20:27

I'm looking for an Objective-C way of sorting characters in a string, as per the answer to this question.

根据这个问题的答案,我正在寻找一种在字符串中对字符进行排序的Objective-C方法。

Ideally a function that takes an NSString and returns the sorted equivalent.

理想情况下,一个函数接受NSString并返回已排序的等价物。

Additionally I'd like to run length encode sequences of 3 or more repeats. So, for example "mississippi" first becomes "iiiimppssss", and then could be shortened by encoding as "4impp4s".

另外,我想运行3次或更多次重复的长度编码序列。因此,例如“mississippi”首先变成“iiiimppssss”,然后可以通过编码缩短为“4impp4s”。

I'm not expert in Objective-C (more Java and C++ background) so I'd also like some clue as to what is the best practice for dealing with the memory management (retain counts etc - no GC on the iphone) for the return value of such a function. My source string is in an iPhone search bar control and so is an NSString *.

我不是Objective-C(更多的Java和C ++背景)的专家,所以我也想知道什么是处理内存管理的最佳实践(保留计数等 - 在iphone上没有GC)返回这样一个函数的值。我的源字符串在iPhone搜索栏控件中,因此是NSString *。

2 个解决方案

#1


int char_compare(const char* a, const char* b) {
    if(*a < *b) {
        return -1;
    } else if(*a > *b) {
        return 1;
    } else {
        return 0;
    }
}

NSString *sort_str(NSString *unsorted) {
    int len = [unsorted length] + 1;
    char *cstr = malloc(len);
    [unsorted getCString:cstr maxLength:len encoding:NSISOLatin1StringEncoding];
    qsort(cstr, len - 1, sizeof(char), char_compare);
    NSString *sorted = [NSString stringWithCString:cstr encoding:NSISOLatin1StringEncoding];
    free(cstr);
    return sorted;
}

The return value is autoreleased so if you want to hold on to it in the caller you'll need to retain it. Not Unicode safe.

返回值是自动释放的,所以如果你想在调用者中保持它,你需要保留它。不是Unicode安全的。

#2


With a bounded code-set, radix sort is best:

使用有界代码集,基数排序是最好的:

NSString * sortString(NSString* word) {
    int rads[128];
    const char *cstr = [word UTF8String];
    char *buff = calloc([word length]+1, sizeof(char));
    int p = 0;
    for(int c = 'a'; c <= 'z'; c++) {
        rads[c] = 0;
    }
    for(int k = 0; k < [word length]; k++) {
        int c = cstr[k];
        rads[c]++;
    }
    for(int c = 'a'; c <= 'z'; c++) {
        int n = rads[c];
        while (n > 0) {
            buff[p++] = c;
            n--;
        }
    }
    buff[p++] = 0;
    return [NSString stringWithUTF8String: buff];
}

Note that the example above only works for lowercase letters (copied from a specific app which needs to sort lowercase strings). To expand it to handle all of the ASCII 127, just do for(c=0; c <= 127; c++).

请注意,上面的示例仅适用于小写字母(从需要对小写字符串排序的特定应用程序复制)。要扩展它以处理所有ASCII 127,只需执行(c = 0; c <= 127; c ++)。

#1


int char_compare(const char* a, const char* b) {
    if(*a < *b) {
        return -1;
    } else if(*a > *b) {
        return 1;
    } else {
        return 0;
    }
}

NSString *sort_str(NSString *unsorted) {
    int len = [unsorted length] + 1;
    char *cstr = malloc(len);
    [unsorted getCString:cstr maxLength:len encoding:NSISOLatin1StringEncoding];
    qsort(cstr, len - 1, sizeof(char), char_compare);
    NSString *sorted = [NSString stringWithCString:cstr encoding:NSISOLatin1StringEncoding];
    free(cstr);
    return sorted;
}

The return value is autoreleased so if you want to hold on to it in the caller you'll need to retain it. Not Unicode safe.

返回值是自动释放的,所以如果你想在调用者中保持它,你需要保留它。不是Unicode安全的。

#2


With a bounded code-set, radix sort is best:

使用有界代码集,基数排序是最好的:

NSString * sortString(NSString* word) {
    int rads[128];
    const char *cstr = [word UTF8String];
    char *buff = calloc([word length]+1, sizeof(char));
    int p = 0;
    for(int c = 'a'; c <= 'z'; c++) {
        rads[c] = 0;
    }
    for(int k = 0; k < [word length]; k++) {
        int c = cstr[k];
        rads[c]++;
    }
    for(int c = 'a'; c <= 'z'; c++) {
        int n = rads[c];
        while (n > 0) {
            buff[p++] = c;
            n--;
        }
    }
    buff[p++] = 0;
    return [NSString stringWithUTF8String: buff];
}

Note that the example above only works for lowercase letters (copied from a specific app which needs to sort lowercase strings). To expand it to handle all of the ASCII 127, just do for(c=0; c <= 127; c++).

请注意,上面的示例仅适用于小写字母(从需要对小写字符串排序的特定应用程序复制)。要扩展它以处理所有ASCII 127,只需执行(c = 0; c <= 127; c ++)。